|
Compound Commands
Up
Next
Previous
Compound Commands
A compound command\fP is one of the following:
-
(list\fP)
-
list\fP is executed in a subshell. Variable assignments and builtin
commands that affect the shell's environment do not remain in effect
after the command completes. The return status is the exit status of
list\fP.
-
{ list\fP; }
-
list\fP is simply executed in the current shell environment.
list\fP must be terminated with a newline or semicolon.
This is known as a group command\fP.
The return status is the exit status of
list\fP.
-
((expression\fP))
-
The expression\fP is evaluated according to the rules described
below under
.SM
.BR "ARITHMETIC EVALUATION" .
If the value of the expression is non-zero, the return status is 0;
otherwise the return status is 1. This is exactly equivalent to
let "expression\fP".
-
[[\fP expression\fP ]]\fP
-
Return a status of 0 or 1 depending on the evaluation of
the conditional expression expression\fP.
Expressions are composed of the primaries described below under
.SM
.BR "CONDITIONAL EXPRESSIONS" .
Word splitting and pathname expansion are not performed on the words
between the [[\fP and ]]\fP; tilde expansion, parameter and
variable expansion, arithmetic expansion, command substitution, process
substitution, and quote removal are performed.
.if t .sp 0.5
.if n .sp 1
When the ==\fP and !=\fP operators are used, the string to the
right of the operator is considered a pattern and matched according
to the rules described below under Pattern Matching\fP.
The return value is 0 if the string matches or does not match
the pattern, respectively, and 1 otherwise.
Any part of the pattern may be quoted to force it to be matched as a
string.
.if t .sp 0.5
.if n .sp 1
Expressions may be combined using the following operators, listed
in decreasing order of precedence:
.if t .sp 0.5
.if n .sp 1
-
( expression\fP )
-
Returns the value of expression\fP.
This may be used to override the normal precedence of operators.
-
! expression\fP
-
True if
expression
is false.
-
expression1\fP &&\fP expression2\fP
-
True if both
expression1
and
expression2
are true.
-
.if t expression1\fP \(bv\(bv\fP expression2\fP
-
.if n expression1\fP ||\fP expression2\fP
True if either
expression1
or
expression2
is true.
.LP
The &&\fP and
.if t \(bv\(bv\fP
.if n ||\fP
operators do not execute expression2\fP if the value of
expression1\fP is sufficient to determine the return value of
the entire conditional expression.
-
for\fP name\fP [ in\fP word\fP ] ; do\fP list\fP ; done\fP
-
The list of words following in\fP is expanded, generating a list
of items.
The variable name\fP is set to each element of this list
in turn, and list\fP is executed each time.
If the in\fP word\fP is omitted, the for\fP command executes
list\fP once for each positional parameter that is set (see
.SM
PARAMETERS
below).
The return status is the exit status of the last command that executes.
If the expansion of the items following in\fP results in an empty
list, no commands are executed, and the return status is 0.
-
for\fP (( expr1\fP ; expr2\fP ; expr3\fP )) ; do\fP list\fP ; done\fP
-
First, the arithmetic expression expr1\fP is evaluated according
to the rules described below under
.SM
.BR "ARITHMETIC EVALUATION" .
The arithmetic expression expr2\fP is then evaluated repeatedly
until it evaluates to zero.
Each time expr2\fP evaluates to a non-zero value, list\fP is
executed and the arithmetic expression expr3\fP is evaluated.
If any expression is omitted, it behaves as if it evaluates to 1.
The return value is the exit status of the last command in list\fP
that is executed, or false if any of the expressions is invalid.
-
select\fP name\fP [ in\fP word\fP ] ; do\fP list\fP ; done\fP
-
The list of words following in\fP is expanded, generating a list
of items. The set of expanded words is printed on the standard
error, each preceded by a number. If the in\fP
word\fP is omitted, the positional parameters are printed (see
.SM
PARAMETERS
below). The
PS3
prompt is then displayed and a line read from the standard input.
If the line consists of a number corresponding to one of
the displayed words, then the value of
name
is set to that word. If the line is empty, the words and prompt
are displayed again. If EOF is read, the command completes. Any
other value read causes
name
to be set to null. The line read is saved in the variable
.BR REPLY .
The
list
is executed after each selection until a
break
or
return
command is executed.
The exit status of
select
is the exit status of the last command executed in
.IR list ,
or zero if no commands were executed.
-
case\fP word\fP in\fP [ [(] pattern\fP [ |\fP pattern\fP ] \
-
A case\fP command first expands word\fP, and tries to match
it against each pattern\fP in turn, using the same matching rules
as for pathname expansion (see
Pathname Expansion
below). When a match is found, the
corresponding list\fP is executed. After the first match, no
subsequent matches are attempted. The exit status is zero if no
pattern matches. Otherwise, it is the exit status of the
last command executed in list\fP.
-
if\fP list\fP; then\fP list;\fP \
-
[ elif\fP list\fP; then\fP list\fP; ] ... \
[ else\fP list\fP; ] fi\fP
The
if
list
is executed. If its exit status is zero, the
then\fP list\fP is executed. Otherwise, each elif\fP
list\fP is executed in turn, and if its exit status is zero,
the corresponding then\fP list\fP is executed and the
command completes. Otherwise, the else\fP list\fP is
executed, if present. The exit status is the exit status of the
last command executed, or zero if no condition tested true.
-
while\fP list\fP; do\fP list\fP; done\fP
-
-
until\fP list\fP; do\fP list\fP; done\fP
-
The while\fP command continuously executes the do\fP
list\fP as long as the last command in list\fP returns
an exit status of zero. The until\fP command is identical
to the while\fP command, except that the test is negated;
the
do
list
is executed as long as the last command in
list
returns a non-zero exit status.
The exit status of the while\fP and until\fP commands
is the exit status
of the last do\fP list\fP command executed, or zero if
none was executed.
-
[ function\fP ] name\fP () { list\fP; }
-
This defines a function named name\fP. The body\fP of the
function is the
list
of commands between { and }. This list
is executed whenever name\fP is specified as the
name of a simple command. The exit status of a function is
the exit status of the last command executed in the body. (See
.SM
FUNCTIONS
below.)
Up
Next
Previous