From Wikipedia, the free encyclopedia
The Bourne shell, or sh, was the default Unix shell of Unix Version 7, and replaced the Thompson shell, whose executable file had the same name, sh. It was developed by Stephen Bourne, of AT&T Bell Laboratories, and was released in 1977 in the Version 7 Unix release distributed to colleges and universities. It remains a popular default shell for Unix accounts. The binary program of the Bourne shell or a compatible program is located at
/bin/sh on most Unix systems, and is still the default shell for the root superuser on many current Unix implementations.
The shell was designed as a replacement for the original Thompson shell.
Among the primary goals were:
- To allow shell scripts to be used as filters.
- To provide programmability including control flow and variables.
- Control over all input/output file descriptors.
- Control over signal handling within scripts.
- No limits on string lengths when interpreting shell scripts.
- Rationalize and generalize string quoting mechanism.
- The environment mechanism was added to V7. This allowed context to be established at startup and provided a way for shell scripts to pass context to sub scripts (processes) without having to use explicit positional parameters.
Although it is used as an interactive command interpreter, it was always intended as a scripting language. It gained popularity with the publication of The UNIX Programming Environment by Brian W. Kernighan and Rob Pike. This was the first commercially published book that presented the shell as a programming language in a tutorial form.
The Bourne shell also was the first to feature the convention of using file descriptor
2> for error messages, allowing much greater programmatic control during scripting by keeping error messages separate from data.
Other innovations in this shell included:
- Command substitution using back quotes:
- Here documents using
<<<here text) to embed a block of input text within a script.
for ~ do ~ done" loops, in particular the use of
$*to loop over arguments.
case ~ in ~ esac" selection mechanism, primarily intended to assist argument parsing.
shprovided support for environment variables using keyword parameters and exportable variables.
Over the years, the Bourne shell was enhanced at AT&T. The various variants are thus called like the respective AT&T Unix version it was released with (some important variants being Version7, SystemIII, SVR2, SVR3, SVR4). As the shell was never versioned, the only way to identify it was testing its features.
Stephen Bourne carried into this shell some aspects of the ALGOL 68C compiler that he had been working on at Cambridge University. Notably he reused portions of ALGOL 68's "
if ~ then ~ elif ~ else ~ fi", "
case ~ in ~ out ~ esac" and "
for ~ while ~ do ~ od" clauses in the common Unix Bourne shell syntax. Moreover - although the v7 shell is written in C - Bourne took advantage of some macros to give the C source code an ALGOL 68 flavor. These macros co-inspired the IOCCC - International Obfuscated C Code Contest (Co-inspired with the finger command as distributed in Unix version 4.2BSD).
The C shell (csh) was derived not from the Bourne shell but the Thompson shell, was distributed with 4.1BSD, and took advantage of job control features of the BSD kernel. Job control is the ability to stop a program interactively and then restart it later. It was for this reason that the C shell gained popularity as a command interpreter. The C shell used a more "C like" syntax for its programming features that was incompatible with the Bourne shell and purportedly an improvement. Job control functionality was eventually incorporated into later versions of the Bourne shell, as well as most of its descendants.
The Korn shell (ksh) written much later by David Korn, was a middle road between these two shells, with syntax chiefly drawn from the Bourne shell and job control features drawn from the C shell. The functionality of the original Korn Shell (known as ksh88 from the year of its introduction) was used as a basis for the POSIX shell standard; a newer version, ksh93, has been open source since 2000 and is used on some Linux distributions; there is also a clone of ksh88 known as pdksh.
Due to copyright issues surrounding the Bourne Shell as it was used in BSD releases, Kenneth Almquist developed a clone of the Bourne Shell, known as the Almquist Shell and available under the BSD license, which is in use today on some BSD descendants and in low-memory situations on systems such as Linux.
The Bourne shell is now standard on all current branded Unix systems, although historically BSD based systems had many scripts written in csh. Bourne shell scripts can typically be run with bash on Linux or other Unix-like systems.
After Sun released their OpenSolaris variant of the Bourne shell as Open Source, a port of this shell for free Unix flavours (geared for legacy portability and historic interest) has been made available by the Heirloom project. The Solaris/SVR4 version of the Bourne Shell does not have the same ALGOL68ized source code as the original.
- ^ Bourne, Steve (1979-01-12). mac.h - Macros used by Bourne to structure C like Algol68C. AT&T. Retrieved on September 9, 2006.
- ^ Landon Curt Noll, Simon Cooper, Peter Seebach and Leonid A. Broukhis (2004). The IOCCC FAQ - Q/A: How did the IOCCC get started?. www.ioccc.org. Retrieved on September 9, 2006.
- UNIX Shell Script Tutorials & Reference
- UNIX/Linux Bourne/Bash Shell Scripting Tutorial
- The individual members of "The Traditional Bourne Shell Family"
- Actual C source code for the Bourne shell using mac.h macros
- Original Bourne Shell documentation from 1978
- A port of the "heirloom" SVR4 Bourne shell from OpenSolaris to other Unix-like systems