[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
5.1 Simple Commands & Pipelines 5.2 Precommand Modifiers 5.3 Complex Commands 5.4 Alternate Forms For Complex Commands 5.5 Reserved Words 5.6 Comments 5.7 Aliasing 5.8 Quoting
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
echo foo |
is a simple command with arguments.
A pipeline is either a simple command, or a sequence of two or more simple commands where each command is separated from the next by `|' or `|&'. Where commands are separated by `|', the standard output of the first command is connected to the standard input of the next. `|&' is shorthand for `2>&1 |', which connects both the standard output and the standard error of the command to the standard input of the next. The value of a pipeline is the value of the last command, unless the pipeline is preceded by `!' in which case the value is the logical inverse of the value of the last command. For example,
echo foo | sed 's/foo/bar/' |
is a pipeline, where the output (`foo' plus a newline) of the first command will be passed to the input of the second.
If a pipeline is preceded by `coproc', it is executed as a coprocess; a two-way pipe is established between it and the parent shell. The shell can read from or write to the coprocess by means of the `>&p' and `<&p' redirection operators or with `print -p' and `read -p'. A pipeline cannot be preceded by both `coproc' and `!'. If job control is active, the coprocess can be treated in other than input and output as an ordinary background job.
A sublist is either a single pipeline, or a sequence of two or more pipelines separated by `&&' or `||'. If two pipelines are separated by `&&', the second pipeline is executed only if the first succeeds (returns a zero value). If two pipelines are separated by `||', the second is executed only if the first fails (returns a nonzero value). Both operators have equal precedence and are left associative. The value of the sublist is the value of the last pipeline executed. For example,
dmesg | grep panic && print yes |
is a sublist consisting of two pipelines, the second just a simple command which will be executed if and only if the grep command returns a zero value. If it does not, the value of the sublist is that return value, else it is the value returned by the print (almost certainly zero).
A list is a sequence of zero or more sublists, in which each sublist is terminated by `;', `&', `&|', `&!', or a newline. This terminator may optionally be omitted from the last sublist in the list when the list appears as a complex command inside `(...)' or `{...}'. When a sublist is terminated by `;' or newline, the shell waits for it to finish before executing the next sublist. If a sublist is terminated by a `&', `&|', or `&!', the shell executes the last pipeline in it in the background, and does not wait for it to finish (note the difference from other shells which execute the whole sublist in the background). A backgrounded pipeline returns a status of zero.
More generally, a list can be seen as a set of any shell commands whatsoever, including the complex commands below; this is implied wherever the word `list' appears in later descriptions. For example, the commands in a shell function form a special sort of list.
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
More than one parameter name can appear before the list of words. If N names are given, then on each execution of the loop the next N words are assigned to the corresponding parameters. If there are more names than remaining words, the remaining parameters are each set to the empty string. Execution of the loop ends when there is no remaining word to assign to the first name. It is only possible for in to appear as the first name in the list, else it will be treated as marking the end of the list.
Optional newlines or semicolons may appear after the always; note, however, that they may not appear between the preceeding closing brace and the always.
An `error' in this context is a condition such as a syntax error which causes the shell to abort execution of the current function, script, or list. Syntax errors encountered while the shell is parsing the code do not cause the always-list to be executed. For example, an erroneously constructed if block in try-list would cause the shell to abort during parsing, so that always-list would not be executed, while an erroneous substitution such as ${*foo*} would cause a run-time error, after which always-list would be executed.
An error condition can be tested and reset with the special integer variable TRY_BLOCK_ERROR. Outside an always-list the value is irrelevant, but it is initialised to -1. Inside always-list, the value is 1 if an error occurred in the try-list, else 0. If TRY_BLOCK_ERROR is set to 0 during the always-list, the error condition caused by the try-list is reset, and shell execution continues normally after the end of always-list. Altering the value during the try-list is not useful (unless this forms part of an enclosing always block).
Regardless of TRY_BLOCK_ERROR, after the end of always-list the normal shell status $? is the value returned from always-list. This will be non-zero if there was an error, even if TRY_BLOCK_ERROR was set to zero.
The following executes the given code, ignoring any errors it causes. This is an alternative to the usual convention of protecting code by executing it in a subshell.
{ # code which may cause an error } always { # This code is executed regardless of the error. (( TRY_BLOCK_ERROR = 0 )) } # The error condition has been reset. |
An exit command encountered in try-list does not cause the execution of always-list. Instead, the shell exits immediately after any EXIT trap has been executed.
If the option SH_GLOB is set for compatibility with other shells, then whitespace may appear between between the left and right parentheses when there is a single word; otherwise, the parentheses will be treated as forming a globbing pattern in that case.
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
The short versions below only work if sublist is of the form `{ list }' or if the SHORT_LOOPS option is set. For the if, while and until commands, in both these cases the test part of the loop must also be suitably delimited, such as by `[[ ... ]]' or `(( ... ))', else the end of the test will not be recognized. For the for, repeat, case and select commands no such special form for the arguments is necessary, but the other condition (the special form of sublist or use of the SHORT_LOOPS option) still applies.
if [[ -o ignorebraces ]] { print yes } |
works, but
if true { # Does not work! print yes } |
does not, since the test is not suitably delimited.
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
do done esac then elif else fi for case if while function repeat time until select coproc nocorrect foreach end ! [[ { }
Additionally, `}' is recognized in any position if the IGNORE_BRACES option is not set.
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
Alias expansion is done on the shell input before any other expansion except history expansion. Therefore, if an alias is defined for the word foo, alias expansion may be avoided by quoting part of the word, e.g. \foo. But there is nothing to prevent an alias being defined for \foo as well.
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
A string enclosed between `$'' and `'' is processed the same way as the string arguments of the print builtin, and the resulting string is considered to be entirely quoted. A literal `'' character can be included in the string by using the `\'' escape.
All characters enclosed between a pair of single quotes (") that is not preceded by a `$' are quoted. A single quote cannot appear within single quotes unless the option RC_QUOTES is set, in which case a pair of single quotes are turned into a single quote. For example,
print '''' |
outputs nothing apart from a newline if RC_QUOTES is not set, but one single quote if it is set.
Inside double quotes (""), parameter and command substitution occur, and `\' quotes the characters `\', ``', `"', and `$'.
[ << ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |