Chapter 3: Basic Shell Features

9

Each command in a pipeline is executed in its own subshell (see Section 3.7.3 [Command

Execution Environment], page 36). The exit status of a pipeline is the exit status of the last command in the pipeline, unless the pipefail option is enabled (see Section 4.3.1 [The

Set Builtin], page 58). If pipefail is enabled, the pipeline’s return status is the value of the last (rightmost) command to exit with a non-zero status, or zero if all commands exit successfully. If the reserved word ‘!’ precedes the pipeline, the exit status is the logical negation of the exit status as described above. The shell waits for all commands in the pipeline to terminate before returning a value.

3.2.3 Lists of Commands

A list is a sequence of one or more pipelines separated by one of the operators ‘;’, ‘&’,

‘&&’, or ‘||’, and optionally terminated by one of ‘;’, ‘&’, or a newline.

Of these list operators, ‘&&’ and ‘||’ have equal precedence, followed by ‘;’ and ‘&’, which have equal precedence.

A sequence of one or more newlines may appear in a list to delimit commands, equiv- alent to a semicolon.

If a command is terminated by the control operator ‘&’, the shell executes the command asynchronously in a subshell. This is known as executing the command in the background.

The shell does not wait for the command to finish, and the return status is 0 (true). When job control is not active (see Chapter 7 [Job Control], page 97), the standard input for asynchronous commands, in the absence of any explicit redirections, is redirected from

/dev/null.

Commands separated by a ‘;’ are executed sequentially; the shell waits for each command to terminate in turn. The return status is the exit status of the last command executed. and and or lists are sequences of one or more pipelines separated by the control oper- ators ‘&&’ and ‘||’, respectively. and and or lists are executed with left associativity.

An and list has the form command1 && command2 command2 is executed if, and only if, command1 returns an exit status of zero.

An or list has the form command1 || command2 command2 is executed if, and only if, command1 returns a non-zero exit status.

The return status of and and or lists is the exit status of the last command executed in the list.

3.2.4 Compound Commands

Compound commands are the shell programming constructs. Each construct begins with a reserved word or control operator and is terminated by a corresponding reserved word or operator. Any redirections (see Section 3.6 [Redirections], page 31) associated with a compound command apply to all commands within that compound command unless explicitly overridden.

In most cases a list of commands in a compound command’s description may be separated from the rest of the command by one or more newlines, and may be followed by a newline in place of a semicolon.