Go to the first, previous, next, last section, table of contents.

Copyright (C) 1991, 1993 Free Software Foundation, Inc.

Bourne Shell Style Features

Bash is an acronym for Bourne Again SHell. The Bourne shell is the traditional Unix shell originally written by Stephen Bourne. All of the Bourne shell builtin commands are available in Bash, and the rules for evaluation and quoting are taken from the Posix 1003.2 specification for the `standard' Unix shell.

This section briefly summarizes things which Bash inherits from the Bourne shell: shell control structures, builtins, variables, and other features. It also lists the significant differences between Bash and the Bourne Shell.

Looping Constructs

Note that wherever you see a `;' in the description of a command's syntax, it may be replaced indiscriminately with one or more newlines.

Bash supports the following looping constructs.

The syntax of the until command is:
until test-commands; do consequent-commands; done
Execute consequent-commands as long as the final command in test-commands has an exit status which is not zero.
The syntax of the while command is:
while test-commands; do consequent-commands; done
Execute consequent-commands as long as the final command in test-commands has an exit status of zero.
The syntax of the for command is:
for name [in words ...]; do commands; done
Execute commands for each member in words, with name bound to the current member. If "in words" is not present, "in "$@"" is assumed.

Conditional Constructs

The syntax of the if command is:
if test-commands; then
[elif more-test-commands; then
[else alternate-consequents;]
Execute consequent-commands only if the final command in test-commands has an exit status of zero. Otherwise, each elif list is executed in turn, and if its exit status is zero, the corresponding more-consequents is executed and the command completes. If "else alternate-consequents" is present, and the final command in the final if or elif clause has a non-zero exit status, then execute alternate-consequents.
The syntax of the case command is:
case word in [pattern [| pattern]...) commands ;;]... esac
Selectively execute commands based upon word matching pattern. The `|' is used to separate multiple patterns. Here is an example using case in a script that could be used to describe an interesting feature of an animal:
echo -n "Enter the name of an animal: "
echo -n "The $ANIMAL has "
case $ANIMAL in
  horse | dog | cat) echo -n "four";;
  man | kangaroo ) echo -n "two";;
  *) echo -n "an unknown number of";;
echo "legs."

Shell Functions

Shell functions are a way to group commands for later execution using a single name for the group. They are executed just like a "regular" command. Shell functions are executed in the current shell context; no new process is created to interpret them.

Functions are declared using this syntax:

[ function ] name () { command-list; }

This defines a function named name. The body of the function is the command-list between { and }. This list is executed whenever name is specified as the name of a command. The exit status of a function is the exit status of the last command executed in the body.

When a function is executed, the arguments to the function become the positional parameters during its execution. The special parameter # that gives the number of positional parameters is updated to reflect the change. Positional parameter 0 is unchanged.

If the builtin command return is executed in a function, the function completes and execution resumes with the next command after the function call. When a function completes, the values of the positional parameters and the special parameter # are restored to the values they had prior to function execution.

Bourne Shell Builtins

The following shell builtin commands are inherited from the Bourne shell. These commands are implemented as specified by the Posix 1003.2 standard.

Do nothing beyond expanding any arguments and performing redirections.
Read and execute commands from the filename argument in the current shell context.
Exit from a for, while, or until loop.
Change the current working directory.
Resume the next iteration of an enclosing for, while, or until loop.
Print the arguments, separated by spaces, to the standard output.
The arguments are concatenated together into a single command, which is then read and executed.
If a command argument is supplied, it replaces the shell. If no command is specified, redirections may be used to affect the current shell environment.
Exit the shell.
Mark the arguments as variables to be passed to child processes in the environment.
Parse options to shell scripts or functions.
Remember the full pathnames of commands specified as arguments, so they need not be searched for on subsequent invocations.
Send a signal to a process.
Print the current working directory.
Read a line from the shell input and use it to set the values of specified variables.
Mark variables as unchangable.
Cause a shell function to exit with a specified value.
Shift positional parameters to the left.
Evaluate a conditional expression.
Print out the user and system times used by the shell and its children.
Specify commands to be executed when the shell receives signals.
Set the shell process's file creation mask.
Cause shell variables to disappear.
Wait until child processes exit and report their exit status.

Bourne Shell Variables

Bash uses certain shell variables in the same way as the Bourne shell. In some cases, Bash assigns a default value to the variable.

A list of characters that separate fields; used when the shell splits words as part of expansion.
A colon-separated list of directories in which the shell looks for commands.
The current user's home directory.
A colon-separated list of directories used as a search path for the cd command.
A colon-separated list of files which the shell periodically checks for new mail. You can also specify what message is printed by separating the file name from the message with a `?'. When used in the text of the message, $_ stands for the name of the current mailfile.
The primary prompt string.
The secondary prompt string.
The index of the last option processed by the getopts builtin.
The value of the last option argument processed by the getopts builtin.

Other Bourne Shell Features

Bash implements essentially the same grammar, parameter and variable expansion, redirection, and quoting as the Bourne Shell. Bash uses the Posix 1003.2 standard as the specification of how these features are to be implemented. There are some differences between the traditional Bourne shell and the Posix standard; this section quickly details the differences of significance. A number of these differences are explained in greater depth in subsequent sections.

Major Differences from the Bourne Shell

Bash implements the ! keyword to negate the return value of a pipeline. Very useful when an if statement needs to act only if a test fails.

Bash includes brace expansion (see section Brace Expansion).

Bash includes the Posix and ksh-style pattern removal %% and ## constructs to remove leading or trailing substrings from variables.

The Posix and ksh-style $() form of command substitution is implemented, and preferred to the Bourne shell's " (which is also implemented for backwards compatibility).

Variables present in the shell's initial environment are automatically exported to child processes. The Bourne shell does not normally do this unless the variables are explicitly marked using the export command.

The expansion ${#xx}, which returns the length of $xx, is supported.

The IFS variable is used to split only the results of expansion, not all words. This closes a longstanding shell security hole.

It is possible to have a variable and a function with the same name; sh does not separate the two name spaces.

Bash functions are permitted to have local variables, and thus useful recursive functions may be written.

The noclobber option is available to avoid overwriting existing files with output redirection.

Bash allows you to write a function to override a builtin, and provides access to that builtin's functionality within the function via the builtin and command builtins.

The command builtin allows selective disabling of functions when command lookup is performed.

Individual builtins may be enabled or disabled using the enable builtin.

Functions may be exported to children via the environment.

The Bash read builtin will read a line ending in \ with the -r option, and will use the $REPLY variable as a default if no arguments are supplied.

The return builtin may be used to abort execution of scripts executed with the . or source builtins.

The umask builtin allows symbolic mode arguments similar to those accepted by chmod.

The test builtin is slightly different, as it implements the Posix 1003.2 algorithm, which specifies the behavior based on the number of arguments.

Go to the first, previous, next, last section, table of contents.