Go to the next section.
Copyright (C) 1991, 1993 Free Software Foundation, Inc.
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.
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
until
command is:
until test-commands; do consequent-commands; doneExecute consequent-commands as long as the final command in test-commands has an exit status which is not zero.
while
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
for name [in words ...]; do commands; doneExecute commands for each member in words, with name bound to the current member. If "
in words
" is not
present, "in "$@"
" is assumed.
if
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
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 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.
The following shell builtin commands are inherited from the Bourne shell. These commands are implemented as specified by the Posix 1003.2 standard.
:
.
break
for
, while
, or until
loop.
cd
continue
for
, while
,
or until
loop.
echo
eval
exec
exit
export
getopts
hash
kill
pwd
read
readonly
return
shift
test
[
times
trap
umask
unset
wait
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
PATH
HOME
CDPATH
cd
command.
MAILPATH
$_
stands for the name of the current mailfile.
PS1
PS2
OPTIND
getopts
builtin.
OPTARG
getopts
builtin.
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.
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 next section.