about summary refs log tree commit diff
path: root/Doc/Zsh/grammar.yo
blob: e0602d5ff9cce9d2d01e5c810b7509219ba6deb0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
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 value).  If two pipelines are separated by `tt(||)', 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,

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
value.  If it does not, the value of the sublist is that return value, else
it is the value 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 value is zero,
the tt(then) var(list) is executed.
If each tt(elif) var(list) returns nonzero, 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(subshells)
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).
)
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)
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(testing conditional expression)
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.
)
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.

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($)'.