Add a Binary Payload to your Shell Scripts


Generally when we think of shell scripts we think of editable text, but it's possible to add binary data to your shell script as well. In this case we're going to talk about adding a binary payload to the end of your shell script.

Adding a binary payload to a shell script could, for instance, be used to create a single file shell script that installs your entire software package which could be composed of hundreds of files. You merely append the tar or gzip file of your package as a binary payload to the script file, when the script runs it extracts the payload and does its task with the extracted files.

For this example I assume the appended file is a tar.gz file. The payload is appended to the end of an installation script preceded by a marker line (PAYLOAD:). The appended data is either uuencoded or just binary data. The script that follows takes a single argument which should be the tar.gz to append to the installation script. The installation script template is copied to with the payload appended. This script is named follows:


# Check for payload format option (default is uuencode).
if [[ "$1" == '--binary' ]]; then
if [[ "$1" == '--uuencode' ]]; then

if [[ ! "$1" ]]; then
	echo "Usage: $0 [--binary | --uuencode] PAYLOAD_FILE"
	exit 1

if [[ $binary -ne 0 ]]; then
	# Append binary data.
	sed \
		-e 's/uuencode=./uuencode=0/' \
		-e 's/binary=./binary=1/' \ >
	echo "PAYLOAD:" >>

	cat $1 >>
if [[ $uuencode -ne 0 ]]; then
	# Append uuencoded data.
	sed \
		-e 's/uuencode=./uuencode=1/' \
		-e 's/binary=./binary=0/' \ >
	echo "PAYLOAD:" >>

	cat $1 | uuencode - >>

In addition to appending the payload it also modifies the installer script to tell it whether the payload is binary or uuencoded.

The template script is out installation script which at this point just untars the payload and nothing else. Actually, it doesn't even untar the payload it just tests it with tar's -t option:



function untar_payload()
	match=$(grep --text --line-number '^PAYLOAD:$' $0 | cut -d ':' -f 1)
	payload_start=$((match + 1))
	if [[ $binary -ne 0 ]]; then
		tail -n +$payload_start $0 | tar -tzvf -
	if [[ $uuencode -ne 0 ]]; then
		tail -n +$payload_start $0 | uudecode | tar -tzvf -

read -p "Install files? " ans
if [[ "${ans:0:1}"  ||  "${ans:0:1}" ]]; then
	# Do remainder of install steps.

exit 0

In the function untar_payload the script uses grep to search throught itself ($0) for the marker and then it extracts the line number from the grep output and adds one to it. This line number is then passed to tail preceded by a plus sign which causes tail to output everything starting at that line number. The data is then fed directly into tar for extraction if the payload is binary. If it's uuencoded then it's first fed into uudecode before being fed into tar.

To create our installer let's use a simple payload file that contains three files name a, b, and c. We'll add the payload as an uuencoded block:

$ sh --uuencode abc.tar.gz
$ cat

... # Installer script lines (see above)
read -p "Install files? " ans
... # More installer script lines (see above)
exit 0

begin 644 -

At the end of the file you see the PAYLOAD: marker and the uuencoded block. If we now run the script we get:

$ sh
Install files? y
-rw-r--r-- mitch/users       0 2009-02-18 11:29 a
-rw-r--r-- mitch/users       0 2009-02-18 11:29 b
-rw-r--r-- mitch/users       0 2009-02-18 11:29 c

I won't show you the --binary usage but it produces the same result, albeit with a slightly smaller foot print since the payload does not have to be uuencoded.


Mitch Frazier is an Associate Editor for Linux Journal.


Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Good Article

Barun Saha's picture

This is a very good article, Mitch.

bzip as well

Tim Parks's picture

In addition to bnary, it seems like the technique would work for compressed data as well (e.g., bzip2).

bzip2 --stdout $1 >>

Of course the would have to be changed to account for the compressed information.

Nice article.

cat $1 or cat $2 ?

Libu's picture

Shouldn't the two cat's have been "cat $2 " ??

cat $2 >>
cat $2 | uuencode - >>

$1 is correct - you must

Jon Brett's picture

$1 is correct - you must have skipped-over the "shift" operation when you read the script.

yup, the shift just didn't

Anonymous's picture

yup, the shift just didn't register.


minor change

John McKown's picture

You have two "cat" lines in your script:

cat $1 >>


cat $1 | uuencode - >>

In the first case, I think it would be safer to do:

cat "$1" >>

In the second case, I would eliminate the cat entirely:

uuencode "$1" - >>

The main change is quoting the $1 just in case the file name contains something that the shell would interpret.

John McKown


Mitch Frazier's picture

Good changes. I don't use uuencode very often and I didn't find the man page very enlightening. That was the first syntax I stumbled upon that did what I wanted.

As far as quoting "$1" goes, you're not gonna put spaces in the file name or something strange like that are you?

Mitch Frazier is an Associate Editor for Linux Journal.

Me, personally? No, way. But

John McKown's picture

Me, personally? No, way. But I've actually got a vendor product which was ported from Windows which has file and subdirectory names with embedded blanks and dollar signs.


Anonymous's picture

Typo in your on line 10. Missing $ before "binary"


Mitch Frazier's picture

Fixed. Strange, it didn't fail when I tested it.

Mitch Frazier is an Associate Editor for Linux Journal.

Cool. Similar to makeself

Anonymous's picture

Cool. Similar to makeself and shar.

Geek Guide
The DevOps Toolbox

Tools and Technologies for Scale and Reliability
by Linux Journal Editor Bill Childers

Get your free copy today

Sponsored by IBM

Upcoming Webinar
8 Signs You're Beyond Cron

Scheduling Crontabs With an Enterprise Scheduler
11am CDT, April 29th
Moderated by Linux Journal Contributor Mike Diehl

Sign up now

Sponsored by Skybot