hpr4587 :: UNIX Curio #1 - Shell Archives
Putting an archive into a self-extracting shell script
Hosted by Vance on Tuesday, 2026-03-03 is flagged as Clean and is released under a CC-BY-SA license.
unix curio, unix, archive, shar.
(Be the first).
Listen in ogg,
opus,
or mp3 format. Play now:
Duration: 00:12:08
Download the transcription and
subtitles.
general.
This is the first column in a series dedicated to exploring little-known—and occasionally useful—trinkets lurking in the dusty corners of UNIX-like operating systems.
This month's column was inspired by an article on the Linux Journal web site 1 describing a custom-built script that would contain a binary tar archive and, when run, would extract the contents onto the user's system. Upon reading this, memories immediately came rushing back of the days of Usenet, before MIME-encoded e-mail made sending file attachments standard 2 , and where we walked ten miles each way to school (uphill both ways!) in three feet of snow.
Yes, at that time, you had to put everything into the body of your message. But what if you needed to send a bunch of files to someone? There was
tar
, but the format differed between systems, and e-mail and Usenet could only reliably handle 7-bit plain-text ASCII anyhow. You could send separate e-mail messages (but what if one goes missing?) or put "CUT HERE" lines to designate where one file ends and another one begins (tedious for the recipient). The solution was a shell archive created by the
shar
program. This wraps all your files in a neat shell script that the recipient can just run and have the files magically pop out. All he needs is the Bourne shell and the
sed
utility, both standard on any UNIX-like system.
Suppose you had a directory named "foo" containing the files bar.c, bar.h, and bar.txt, and wanted to send these. All you'd need to do is run the following command, and your archive is on its way.
$ shar foo foo/* | mail -s "Foo 1.0 files" bob@example.com
When the recipient runs the resulting script, it will create the foo directory and copy out the files onto his system. You can also pick and choose files; if you wanted to leave out bar.txt, you could do
shar foo foo/bar.c foo/bar.h
or, more simply,
shar foo foo/bar.?
.
Different versions of
shar
have varying capabilities. For example, the
BSD
3
and
OS X
4
editions can only really manage plain-text files. If you had a binary object file bar.o, it'd likely get mangled somewhere along the way if you tried to include it in an archive. They also require, as in the examples above, that you name a directory before naming any files inside it (the typical way is to let the
find
command do the work for you; it produces a list in the right order).
The GNU implementation is more flexible and can take just a directory name, automatically including everything underneath. It can also handle binary files by using uuencode—a method for encoding data as ASCII that predated the current base64 MIME standard. GNU
shar
rather nicely auto-detects whether the input file is text or binary and acts accordingly, and can even compress files if asked. However, unpacking encoded or compressed files from such an archive requires the recipient to have the corresponding decode/uncompress utility, and the
documentation is littered with (now somewhat anachronistic) warnings about this
5
.
Looking at other UNIX systems,
the HP-UX version
6
also can uuencode binary files, and as a special bonus adds logic to the script that will compile and use a simple uudecode tool if the recipient doesn't already have one. It will even handle device files and put the corresponding
mknod
commands into the script, probably making it the most full-featured implementation of all. IBM's AIX doesn't appear to come with
shar
. Neither do SunOS and Solaris, which seems quite odd as original development of the program is
credited to James Gosling
5
!
And so we bid farewell to
shar
. Next time you're considering rolling your own script for a particular purpose, consider whether such a tool might already exist, just waiting on your system for you to use it.
References:
- Add a Binary Payload to your Shell Scripts https://www.linuxjournal.com/content/add-binary-payload-your-shell-scripts
- MIME (Multipurpose Internet Mail Extensions) Part One https://datatracker.ietf.org/doc/html/rfc1521
- BSD shar manual page https://man.freebsd.org/cgi/man.cgi?query=shar&sektion=1&manpath=4.4BSD+Lite2
- macOS 26.2 shar manual page https://man.freebsd.org/cgi/man.cgi?query=shar&sektion=1&manpath=macOS+26.2
- GNU shar utilities manual https://www.gnu.org/software/sharutils/manual/sharutils.html
- HP-UX Reference (11i v3 07/02) - 1 User Commands N-Z (vol 2) https://support.hpe.com/hpesc/public/docDisplay?docId=c01922474&docLocale=en_US
This article was originally written in May 2010. The podcast episode was recorded in February 2026.