Next: , Up: Portable Shell


10.1 Shellology

There are several families of shells, most prominently the Bourne family and the C shell family which are deeply incompatible. If you want to write portable shell scripts, avoid members of the C shell family. The the Shell difference FAQ includes a small history of Unix shells, and a comparison between several of them.

Below we describe some of the members of the Bourne shell family.

Ash
ash is often used on GNU/Linux and BSD systems as a light-weight Bourne-compatible shell. Ash 0.2 has some bugs that are fixed in the 0.3.x series, but portable shell scripts should work around them, since version 0.2 is still shipped with many GNU/Linux distributions.

To be compatible with Ash 0.2:


Bash
To detect whether you are running bash, test if BASH_VERSION is set. To disable its extensions and require POSIX compatibility, run `set -o posix'. See Bash POSIX Mode, for details.
Bash 2.05 and later
Versions 2.05 and later of bash use a different format for the output of the set builtin, designed to make evaluating its output easier. However, this output is not compatible with earlier versions of bash (or with many other shells, probably). So if you use bash 2.05 or higher to execute configure, you'll need to use bash 2.05 for all other build tasks as well.
Ksh
The Korn shell is compatible with the Bourne family and it mostly conforms to POSIX. It has two major variants commonly called `ksh88' and `ksh93', named after the years of initial release. It is usually called ksh, but Solaris systems have three variants: /usr/bin/ksh is `ksh88', /usr/xpg4/bin/sh is a POSIX-compliant variant of `ksh88', and /usr/dt/bin/dtksh is `ksh93'. /usr/bin/ksh is standard on Solaris; the other variants are parts of optional packages. There is no extra charge for these packages, but they are not part of a minimal OS install and therefore some installations may not have it. A public-domain clone of the Korn shell called `pdksh' is also widely available: it has most of the `ksh88' features along with a few of its own.
Zsh
To detect whether you are running zsh, test if ZSH_VERSION is set. By default zsh is not compatible with the Bourne shell: you have to run `emulate sh' and set NULLCMD to `:'. See Compatibility, for details.

Zsh 3.0.8 is the native /bin/sh on Mac OS X 10.0.3.

The following discussion between Russ Allbery and Robert Lipe is worth reading:

Russ Allbery:

The GNU assumption that /bin/sh is the one and only shell leads to a permanent deadlock. Vendors don't want to break users' existing shell scripts, and there are some corner cases in the Bourne shell that are not completely compatible with a POSIX shell. Thus, vendors who have taken this route will never (OK...“never say never”) replace the Bourne shell (as /bin/sh) with a POSIX shell.

Robert Lipe:

This is exactly the problem. While most (at least most System V's) do have a Bourne shell that accepts shell functions most vendor /bin/sh programs are not the POSIX shell.

So while most modern systems do have a shell somewhere that meets the POSIX standard, the challenge is to find it.