1
0
mirror of git://git.code.sf.net/p/zsh/code synced 2024-11-19 13:33:52 +01:00
zsh/Doc/Zsh/grammar.yo

472 lines
19 KiB
Plaintext

texinode(Shell Grammar)(Redirection)(Files)(Top)
chapter(Shell Grammar)
cindex(shell grammar)
cindex(grammar, shell)
startmenu()
menu(Simple Commands & Pipelines)
menu(Precommand Modifiers)
menu(Complex Commands)
menu(Alternate Forms For Complex Commands)
menu(Reserved Words)
menu(Comments)
menu(Aliasing)
menu(Quoting)
endmenu()
texinode(Simple Commands & Pipelines)(Precommand Modifiers)()(Shell Grammar)
sect(Simple Commands & Pipelines)
cindex(simple commands)
cindex(commands, simple)
A em(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,
example(echo foo)
is a simple command with arguments.
cindex(pipeline)
A em(pipeline) is either a simple command, or a sequence of two or more
simple commands where each command is separated from the next by `tt(|)'
or `tt(|&)'. Where commands are separated by `tt(|)', the standard
output of the first command is connected to the
standard input of the next. `tt(|&)' is shorthand for `tt(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
`tt(!)' in which case the value is the logical inverse of the value of the
last command.
For example,
example(echo foo | sed 's/foo/bar/')
is a pipeline, where the output (`tt(foo)' plus a newline) of the first
command will be passed to the input of the second.
findex(coproc)
cindex(coprocess)
If a pipeline is preceded by `tt(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 `tt(>&p)'
and `tt(<&p)' redirection operators or with `tt(print -p)' and `tt(read -p)'.
A pipeline cannot be preceded by both `tt(coproc)' and `tt(!)'.
If job control is active, the coprocess can be treated in other than input
and output as an ordinary background job.
cindex(sublist)
A em(sublist) is either a single pipeline, or a sequence of two or more
pipelines separated by `tt(&&)' or `tt(||)'. If two pipelines are separated
by `tt(&&)', the second pipeline is executed only if the first succeeds
(returns a zero status). If two pipelines are separated by `tt(||)', the
second is executed only if the first fails (returns a nonzero status).
Both operators have equal precedence and are left associative.
The value of the sublist is the value of the last pipeline executed.
For example,
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 tt(grep) command returns a zero
status. If it does not, the value of the sublist is that return status, else
it is the status returned by the tt(print) (almost certainly zero).
cindex(list)
A em(list) is a sequence of zero or more sublists, in which each sublist
is terminated by `tt(;)', `tt(&)', `tt(&|)', `tt(&!)', 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 `tt(LPAR())...tt(RPAR())'
or `tt({)...tt(})'. When a
sublist is terminated by `tt(;)' or newline, the shell waits for it to
finish before executing the next sublist. If a sublist is terminated
by a `tt(&)', `tt(&|)', or `tt(&!)',
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.
texinode(Precommand Modifiers)(Complex Commands)(Simple Commands & Pipelines)(Shell Grammar)
sect(Precommand Modifiers)
cindex(precommand modifiers)
cindex(modifiers, precommand)
A simple command may be preceded by a em(precommand modifier),
which will alter how the command is interpreted. These modifiers are
shell builtin commands with the exception of tt(nocorrect) which is
a reserved word.
startitem()
item(tt(-))(
The command is executed with a `tt(-)' prepended to its
tt(argv[0]) string.
)
item(tt(noglob))(
Filename generation (globbing) is not performed on any of
the words.
)
item(tt(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.
)
item(tt(exec))(
The command is executed in the parent shell without forking.
)
item(tt(command))(
The command word is taken to be the name of an external command,
rather than a shell function or builtin.
)
item(tt(builtin))(
The command word is taken to be the name of a builtin command,
rather than a shell function or external command.
)
enditem()
texinode(Complex Commands)(Alternate Forms For Complex Commands)(Precommand Modifiers)(Shell Grammar)
sect(Complex Commands)
cindex(complex commands)
cindex(commands, complex)
A em(complex command) in zsh is one of the following:
startitem()
findex(if)
cindex(if construct)
item(tt(if) var(list) tt(then) var(list) [ tt(elif) var(list) tt(then) var(list) ] ... [ tt(else) var(list) ] tt(fi))(
The tt(if) var(list) is executed, and if it returns a zero exit status,
the tt(then) var(list) is executed.
Otherwise, the tt(elif) var(list) is executed and if its status is zero,
the tt(then) var(list) is executed.
If each tt(elif) var(list) returns nonzero status, the tt(else) var(list)
is executed.
)
findex(for)
cindex(for loops)
cindex(loops, for)
item(tt(for) var(name) ... [ tt(in) var(word) ... ] var(term) tt(do) var(list) tt(done))(
where var(term) is at least one newline or tt(;).
Expand the list of var(word)s, and set the parameter
var(name) to each of them in turn, executing
var(list) each time. If the tt(in) var(word) is omitted,
use the positional parameters instead of the var(word)s.
More than one parameter var(name) can appear before the list of
var(word)s. If var(N) var(name)s are given, then on each execution of the
loop the next tt(N) var(word)s are assigned to the corresponding
parameters. If there are more var(name)s than remaining var(word)s, the
remaining parameters are each set to the empty string. Execution of the
loop ends when there is no remaining var(word) to assign to the first
var(name). It is only possible for tt(in) to appear as the first var(name)
in the list, else it will be treated as marking the end of the list.
)
item(tt(for LPAR()LPAR()) [var(expr1)] tt(;) [var(expr2)] tt(;) [var(expr3)] tt(RPAR()RPAR() do) var(list) tt(done))(
The arithmetic expression var(expr1) is evaluated first (see
noderef(Arithmetic Evaluation)). The arithmetic expression
var(expr2) is repeatedly evaluated until it evaluates to zero and
when non-zero, var(list) is executed and the arithmetic expression
var(expr3) evaluated. If any expression is omitted, then it behaves
as if it evaluated to 1.
)
findex(while)
cindex(while loops)
cindex(loops, while)
item(tt(while) var(list) tt(do) var(list) tt(done))(
Execute the tt(do) var(list) as long as the tt(while) var(list)
returns a zero exit status.
)
findex(until)
cindex(until loops)
cindex(loops, until)
item(tt(until) var(list) tt(do) var(list) tt(done))(
Execute the tt(do) var(list) as long as tt(until) var(list)
returns a nonzero exit status.
)
findex(repeat)
cindex(repeat loops)
cindex(loops, repeat)
item(tt(repeat) var(word) tt(do) var(list) tt(done))(
var(word) is expanded and treated as an arithmetic expression,
which must evaluate to a number var(n).
var(list) is then executed var(n) times.
)
findex(case)
cindex(case selection)
cindex(selection, case)
item(tt(case) var(word) tt(in) [ [tt(LPAR())] var(pattern) [ tt(|) var(pattern) ] ... tt(RPAR()) var(list) (tt(;;)|tt(;&)) ] ... tt(esac))(
Execute the var(list) associated with the first var(pattern)
that matches var(word), if any. The form of the patterns
is the same as that used for filename generation. See
noderef(Filename Generation).
If the var(list) that is executed is terminated with tt(;&) rather than
tt(;;), the following list is also executed. This continues until either
a list is terminated with tt(;;) or the tt(esac) is reached.
)
findex(select)
cindex(user selection)
cindex(selection, user)
item(tt(select) var(name) [ tt(in) var(word) ... var(term) ] tt(do) var(list) tt(done))(
where var(term) is one or more newline or tt(;) to terminate the var(word)s.
vindex(REPLY, use of)
Print the set of var(word)s, each preceded by a number.
If the tt(in) var(word) is omitted, use the positional parameters.
The tt(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 var(word)s, then the parameter var(name)
is set to the var(word) corresponding to this number.
If this line is empty, the selection list is printed again.
Otherwise, the value of the parameter var(name) is set to null.
The contents of the line read from standard input is saved
in the parameter tt(REPLY). var(list) is executed
for each selection until a break or end-of-file is encountered.
)
cindex(subshell)
item(tt(LPAR()) var(list) tt(RPAR()))(
Execute var(list) in a subshell. Traps set by the tt(trap) builtin
are reset to their default values while executing var(list).
)
item(tt({) var(list) tt(}))(
Execute var(list).
)
findex(always)
cindex(always blocks)
cindex(try blocks)
item(tt({) var(try-list) tt(} always {) var(always-list) tt(}))(
First execute var(try-list). Regardless of errors, or tt(break),
tt(continue), or tt(return) commands encountered within var(try-list),
execute var(always-list). Execution then continues from the
result of the execution of var(try-list); in other words, any error,
or tt(break), tt(continue), or tt(return) command is treated in the
normal way, as if var(always-list) were not present. The two
chunks of code are referred to as the `try block' and the `always block'.
Optional newlines or semicolons may appear after the tt(always);
note, however, that they may em(not) appear between the preceeding
closing brace and the tt(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 var(always-list) to be executed. For example,
an erroneously constructed tt(if) block in tt(try-list) would cause the
shell to abort during parsing, so that tt(always-list) would not be
executed, while an erroneous substitution such as tt(${*foo*}) would
cause a run-time error, after which tt(always-list) would be executed.
An error condition can be tested and reset with the special integer
variable tt(TRY_BLOCK_ERROR). Outside an tt(always-list) the value is
irrelevant, but it is initialised to tt(-1). Inside tt(always-list), the
value is 1 if an error occurred in the tt(try-list), else 0. If
tt(TRY_BLOCK_ERROR) is set to 0 during the tt(always-list), the error
condition caused by the tt(try-list) is reset, and shell execution
continues normally after the end of tt(always-list). Altering the value
during the tt(try-list) is not useful (unless this forms part of an
enclosing tt(always) block).
Regardless of tt(TRY_BLOCK_ERROR), after the end of tt(always-list) the
normal shell status tt($?) is the value returned from tt(always-list).
This will be non-zero if there was an error, even if tt(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.
example({
# 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 tt(exit) command encountered in tt(try-list) does em(not) cause the
execution of var(always-list). Instead, the shell exits immediately
after any tt(EXIT) trap has been executed.
)
findex(function)
xitem(tt(function) var(word) ... [ tt(()) ] [ var(term) ] tt({) var(list) tt(}))
xitem(var(word) ... tt(()) [ var(term) ] tt({) var(list) tt(}))
item(var(word) ... tt(()) [ var(term) ] var(command))(
where var(term) is one or more newline or tt(;).
Define a function which is referenced by any one of var(word).
Normally, only one var(word) is provided; multiple var(word)s
are usually only useful for setting traps.
The body of the function is the var(list) between
the tt({) and tt(}). See noderef(Functions).
If the option tt(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 var(word); otherwise, the parentheses will be treated as
forming a globbing pattern in that case.
)
cindex(timing)
findex(time)
item(tt(time) [ var(pipeline) ])(
The var(pipeline) is executed, and timing statistics are
reported on the standard error in the form specified
by the tt(TIMEFMT) parameter.
If var(pipeline) is omitted, print statistics about the
shell process and its children.
)
cindex(conditional expression)
findex([[)
item(tt([[) var(exp) tt(]]))(
Evaluates the conditional expression var(exp)
and return a zero exit status if it is true.
See noderef(Conditional Expressions)
for a description of var(exp).
)
enditem()
texinode(Alternate Forms For Complex Commands)(Reserved Words)(Complex Commands)(Shell Grammar)
sect(Alternate Forms For Complex Commands)
cindex(alternate forms for complex commands)
cindex(commands, alternate forms for complex)
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 var(sublist) is of the form `tt({)
var(list) tt(})' or if the tt(SHORT_LOOPS) option is set. For the tt(if),
tt(while) and tt(until) commands, in both these cases the test part of the
loop must also be suitably delimited, such as by `tt([[ ... ]])' or `tt(((
... )))', else the end of the test will not be recognized. For the
tt(for), tt(repeat), tt(case) and tt(select) commands no such special form
for the arguments is necessary, but the other condition (the special form
of var(sublist) or use of the tt(SHORT_LOOPS) option) still applies.
startitem()
item(tt(if) var(list) tt({) var(list) tt(}) [ tt(elif) var(list) tt({) var(list) tt(}) ] ... [ tt(else {) var(list) tt(}) ])(
An alternate form of tt(if). The rules mean that
example(if [[ -o ignorebraces ]] {
print yes
})
works, but
example(if true { # Does not work!
print yes
}
)
does em(not), since the test is not suitably delimited.
)
item(tt(if) var(list) var(sublist))(
A short form of the alternate `if'. The same limitations on the form of
var(list) apply as for the previous form.
)
item(tt(for) var(name) ... tt(LPAR()) var(word) ... tt(RPAR()) var(sublist))(
A short form of tt(for).
)
item(tt(for) var(name) ... [ tt(in) var(word) ... ] var(term) var(sublist))(
where var(term) is at least one newline or tt(;).
Another short form of tt(for).
)
item(tt(for LPAR()LPAR()) [var(expr1)] tt(;) [var(expr2)] tt(;) [var(expr3)] tt(RPAR()RPAR()) var(sublist))(
A short form of the arithmetic tt(for) command.
)
findex(foreach)
item(tt(foreach) var(name) ... tt(LPAR()) var(word) ... tt(RPAR()) var(list) tt(end))(
Another form of tt(for).
)
item(tt(while) var(list) tt({) var(list) tt(}))(
An alternative form of tt(while). Note the limitations on the form of
var(list) mentioned above.
)
item(tt(until) var(list) tt({) var(list) tt(}))(
An alternative form of tt(until). Note the limitations on the form of
var(list) mentioned above.
)
item(tt(repeat) var(word) var(sublist))(
This is a short form of tt(repeat).
)
item(tt(case) var(word) tt({) [ [tt(LPAR())] var(pattern) [ tt(|) var(pattern) ] ... tt(RPAR()) var(list) (tt(;;)|tt(;&)) ] ... tt(}))(
An alternative form of tt(case).
)
item(tt(select) var(name) [ tt(in) var(word) var(term) ] var(sublist))(
where var(term) is at least one newline or tt(;).
A short form of tt(select).
)
enditem()
texinode(Reserved Words)(Comments)(Alternate Forms For Complex Commands)(Shell Grammar)
sect(Reserved Words)
cindex(reserved words)
findex(disable, use of)
The following words are recognized as reserved words when used as the first
word of a command unless quoted or disabled using tt(disable -r):
tt(do done esac then elif else fi for case
if while function repeat time until
select coproc nocorrect foreach end ! [[ { })
Additionally, `tt(})' is recognized in any position if the tt(IGNORE_BRACES) option
is not set.
texinode(Comments)(Aliasing)(Reserved Words)(Shell Grammar)
sect(Comments)
cindex(comments)
pindex(INTERACTIVE_COMMENTS, use of)
vindex(histchars, use of)
In noninteractive shells, or in interactive shells with the
tt(INTERACTIVE_COMMENTS) option set, a word beginning
with the third character of the tt(histchars) parameter
(`tt(#)' by default) causes that word and all the following
characters up to a newline to be ignored.
texinode(Aliasing)(Quoting)(Comments)(Shell Grammar)
sect(Aliasing)
cindex(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.
findex(alias, use of)
cindex(aliases, global)
An alias is defined using the tt(alias) builtin; global aliases
may be defined using the tt(-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 tt(foo), alias expansion
may be avoided by quoting part of the word, e.g. tt(\foo).
But there is nothing to prevent an alias being defined
for tt(\foo) as well.
texinode(Quoting)()(Aliasing)(Shell Grammar)
sect(Quoting)
cindex(quoting)
A character may be var(quoted) (that is, made
to stand for itself) by preceding it with a `tt(\)'.
`tt(\)' followed by a newline is ignored.
A string enclosed between `tt($')' and `tt(')' is
processed the same way as the string arguments of the
tt(print) builtin, and the resulting string is considered to be
entirely quoted. A literal `tt(')' character can be included in the
string by using the `tt(\')' escape.
pindex(RC_QUOTES, use of)
All characters enclosed between a pair of single quotes (tt('')) that
is not preceded by a `tt($)' are quoted. A single quote cannot appear
within single quotes unless the option tt(RC_QUOTES) is set, in which case
a pair of single quotes are turned into a single quote. For example,
example(print '''')
outputs nothing apart from a newline if tt(RC_QUOTES) is not set, but one
single quote if it is set.
Inside double quotes (tt("")), parameter and
command substitution occur, and `tt(\)' quotes the characters
`tt(\)', `tt(`)', `tt(")', and `tt($)'.