There are many different dialects of shell scripts, each with their own quirks, and some with their own syntax entirely. Because of these differences, the road to good shell scripting can be fraught with peril, leading to script failures, misbehavior, and even outright data loss.
To that end, the first lesson you must learn before writing a shell script is that there are two fundamentally different sets of shell script syntax: the Bourne shell syntax and the C shell syntax. The C shell syntax is more comfortable to many C programmers because the syntax is somewhat similar. However, the Bourne shell syntax is significantly more flexible and thus more widely used. For this reason, this document only covers the Bourne shell syntax.
The second hard lesson you will invariably learn is that each dialect of Bourne shell syntax differs slightly. This document includes only pure Bourne shell syntax and a few BASH-specific extensions. Where BASH-specific syntax is used, it is clearly noted.
The terminology and subtle syntactic differences can be confusing—even a bit overwhelming at times; had Dorothy in The Wizard of Oz been a programmer, you might have heard her exclaim, "BASH and ZSH and CSH, Oh My!" Fortunately, once you get the basics, things generally fall into place as long as you avoid using shell-specific features. Stay on the narrow road and your code will be portable.
Some common shells are listed below, grouped by script syntax:
Bourne-compatible shells
sh
bash
zsh
ksh
C-shell-compatible shells
csh
tcsh
bcsh (C shell to Bourne shell translator)
Many of these shells have more than one variation. Most of these variations are denoted by prefixing the name of an existing shell with additional letters that are short for whatever differentiates them from the original shell. For example:
The shell pdksh is a variant of ksh. Being a public domain rewrite of AT&T's ksh, it stands for "Public Domain Korn SHell." (This is a bit of a misnomer, as a few bits are under a BSD-like open source license. However, the name remains.)
The shell tcsh is an extension of csh. It stands for the TENEX C SHell, as some of its enhancements were inspired by the TENEX operating system.
The shell bash is an extension of sh. It stands for the Bourne Again SHell. (Oddly enough, it is not a variation of ash, the Almquist SHell, though both are Bourne shell variants.)
And so on. In general, with the exception of csh and tcsh, it is usually safe to assume that any shell will be compatible with Bourne shell syntax.
Note: Because the C shell syntax is not well suited to scripting beyond a very basic level, this document does not cover C shell variants in depth. For more information, see “She Sells C Shells.”
Last updated: 2008-04-08