[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

5. Shell Grammar

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] [ ? ]

5.1 Simple Commands & Pipelines

A simple command is a sequence of optional parameter assignments followed by blank-separated words, with optional redirections interspersed. The first word is the command to be executed, and the remaining words, if any, are arguments to the command. If a command name is given, the parameter assignments modify the environment of the command when it is executed. The value of a simple command is its exit status, or 128 plus the signal number if terminated by a signal. For example,

 
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] [ ? ]

5.2 Precommand Modifiers

A simple command may be preceded by a precommand modifier, which will alter how the command is interpreted. These modifiers are shell builtin commands with the exception of nocorrect which is a reserved word.

-
The command is executed with a `-' prepended to its argv[0] string.

noglob
Filename generation (globbing) is not performed on any of the words.

nocorrect
Spelling correction is not done on any of the words. This must appear before any other precommand modifier, as it is interpreted immediately, before any parsing is done. It has no effect in non-interactive shells.

exec
The command is executed in the parent shell without forking.

command
The command word is taken to be the name of an external command, rather than a shell function or builtin.

builtin
The command word is taken to be the name of a builtin command, rather than a shell function or external command.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

5.3 Complex Commands

A complex command in zsh is one of the following:

if list then list [ elif list then list ] ... [ else list ] fi
The if list is executed, and if it returns a zero exit status, the then list is executed. Otherwise, the elif list is executed and if its value is zero, the then list is executed. If each elif list returns nonzero, the else list is executed.

for name [ in word ... term ] do list done
where term is at least one newline or ;. Expand the list of words, and set the parameter name to each of them in turn, executing list each time. If the in word is omitted, use the positional parameters instead of the words.

for (( [expr1] ; [expr2] ; [expr3] )) do list done
The arithmetic expression expr1 is evaluated first (see 10. Arithmetic Evaluation). The arithmetic expression expr2 is repeatedly evaluated until it evaluates to zero and when non-zero, list is executed and the arithmetic expression expr3 evaluated. If any expression is omitted, then it behaves as if it evaluated to 1.

while list do list done
Execute the do list as long as the while list returns a zero exit status.

until list do list done
Execute the do list as long as until list returns a nonzero exit status.

repeat word do list done
word is expanded and treated as an arithmetic expression, which must evaluate to a number n. list is then executed n times.

case word in [ [(] pattern [ | pattern ] ... ) list (;;|;&) ] ... esac
Execute the list associated with the first pattern that matches word, if any. The form of the patterns is the same as that used for filename generation. See 13.8 Filename Generation. If the list that is executed is terminated with ;& rather than ;;, the following list is also executed. This continues until either a list is terminated with ;; or the esac is reached.

select name [ in word ... term ] do list done
where term is one or more newline or ; to terminate the words. Print the set of words, each preceded by a number. If the in word is omitted, use the positional parameters. The PROMPT3 prompt is printed and a line is read from the line editor if the shell is interactive and that is active, or else standard input. If this line consists of the number of one of the listed words, then the parameter name is set to the word corresponding to this number. If this line is empty, the selection list is printed again. Otherwise, the value of the parameter name is set to null. The contents of the line read from standard input is saved in the parameter REPLY. list is executed for each selection until a break or end-of-file is encountered.

( list )
Execute list in a subshell. Traps set by the trap builtin are reset to their default values while executing list.

{ list }
Execute list.

function word ... [ () ] [ term ] { list }
word ... () [ term ] { list }
word ... () [ term ] command
where term is one or more newline or ;. Define a function which is referenced by any one of word. Normally, only one word is provided; multiple words are usually only useful for setting traps. The body of the function is the list between the { and }. See 8. Functions.

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.

time [ pipeline ]
The pipeline is executed, and timing statistics are reported on the standard error in the form specified by the TIMEFMT parameter. If pipeline is omitted, print statistics about the shell process and its children.

[[ exp ]]
Evaluates the conditional expression exp and return a zero exit status if it is true. See 11. Conditional Expressions for a description of exp.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

5.4 Alternate Forms For Complex Commands

Many of zsh's complex commands have alternate forms. These particular versions of complex commands should be considered deprecated and may be removed in the future. The versions in the previous section should be preferred instead.

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 list { list } [ elif list { list } ] ... [ else { list } ]
An alternate form of if. The rules mean that

 
if [[ -o ignorebraces ]] {
  print yes
}

works, but

 
if true {  # Does not work!
  print yes
}

does not, since the test is not suitably delimited.

if list sublist
A short form of the alternate `if'. The same limitations on the form of list apply as for the previous form.

for name ( word ... ) sublist
A short form of for.

for name [ in word ... term ] sublist
where term is at least one newline or ;. Another short form of for.

for (( [expr1] ; [expr2] ; [expr3] )) sublist
A short form of the arithmetic for command.

foreach name ( word ... ) list end
Another form of for.

while list { list }
An alternative form of while. Note the limitations on the form of list mentioned above.

until list { list }
An alternative form of until. Note the limitations on the form of list mentioned above.

repeat word sublist
This is a short form of repeat.

case word { [ [(] pattern [ | pattern ] ... ) list (;;|;&) ] ... }
An alternative form of case.

select name [ in word term ] sublist
where term is at least one newline or ;. A short form of select.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

5.5 Reserved Words

The following words are recognized as reserved words when used as the first word of a command unless quoted or disabled using disable -r:

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] [ ? ]

5.6 Comments

In noninteractive shells, or in interactive shells with the INTERACTIVE_COMMENTS option set, a word beginning with the third character of the histchars parameter (`#' by default) causes that word and all the following characters up to a newline to be ignored.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

5.7 Aliasing

Every token in the shell input is checked to see if there is an alias defined for it. If so, it is replaced by the text of the alias if it is in command position (if it could be the first word of a simple command), or if the alias is global. If the text ends with a space, the next word in the shell input is treated as though it were in command position for purposes of alias expansion. An alias is defined using the alias builtin; global aliases may be defined using the -g option to that builtin.

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] [ ? ]

5.8 Quoting

A character may be quoted (that is, made to stand for itself) by preceding it with a `\'. `\' followed by a newline is ignored.

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] [ ? ]

This document was generated by Peter Stephenson on October, 24 2001 using texi2html