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.

until
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.
while
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.
for
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

if
The syntax of the if command is:
if test-commands; then
  consequent-commands;
[elif more-test-commands; then
  more-consequents;]
[else alternate-consequents;]
fi
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.
case
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: "
read 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";;
esac
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.
break
Exit from a for, while, or until loop.
cd
Change the current working directory.
continue
Resume the next iteration of an enclosing for, while, or until loop.
echo
Print the arguments, separated by spaces, to the standard output.
eval
The arguments are concatenated together into a single command, which is then read and executed.
exec
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
Exit the shell.
export
Mark the arguments as variables to be passed to child processes in the environment.
getopts
Parse options to shell scripts or functions.
hash
Remember the full pathnames of commands specified as arguments, so they need not be searched for on subsequent invocations.
kill
Send a signal to a process.
pwd
Print the current working directory.
read
Read a line from the shell input and use it to set the values of specified variables.
readonly
Mark variables as unchangable.
return
Cause a shell function to exit with a specified value.
shift
Shift positional parameters to the left.
test
[
Evaluate a conditional expression.
times
Print out the user and system times used by the shell and its children.
trap
Specify commands to be executed when the shell receives signals.
umask
Set the shell process's file creation mask.
unset
Cause shell variables to disappear.
wait
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.

IFS
A list of characters that separate fields; used when the shell splits words as part of expansion.
PATH
A colon-separated list of directories in which the shell looks for commands.
HOME
The current user's home directory.
CDPATH
A colon-separated list of directories used as a search path for the cd command.
MAILPATH
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.
PS1
The primary prompt string.
PS2
The secondary prompt string.
OPTIND
The index of the last option processed by the getopts builtin.
OPTARG
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.