1ZSHEXPN(1) General Commands Manual ZSHEXPN(1)
2
3
4
6 zshexpn - zsh expansion and substitution
7
9 The following types of expansions are performed in the indicated order
10 in five steps:
11
12 History Expansion
13 This is performed only in interactive shells.
14
15 Alias Expansion
16 Aliases are expanded immediately before the command line is
17 parsed as explained under Aliasing in zshmisc(1).
18
19 Process Substitution
20 Parameter Expansion
21 Command Substitution
22 Arithmetic Expansion
23 Brace Expansion
24 These five are performed in one step in left-to-right fashion.
25 After these expansions, all unquoted occurrences of the charac‐
26 ters `\', `'' and `"' are removed.
27
28 Filename Expansion
29 If the SH_FILE_EXPANSION option is set, the order of expansion
30 is modified for compatibility with sh and ksh. In that case
31 filename expansion is performed immediately after alias expan‐
32 sion, preceding the set of five expansions mentioned above.
33
34 Filename Generation
35 This expansion, commonly referred to as globbing, is always done
36 last.
37
38 The following sections explain the types of expansion in detail.
39
41 History expansion allows you to use words from previous command lines
42 in the command line you are typing. This simplifies spelling correc‐
43 tions and the repetition of complicated commands or arguments. Immedi‐
44 ately before execution, each command is saved in the history list, the
45 size of which is controlled by the HISTSIZE parameter. The one most
46 recent command is always retained in any case. Each saved command in
47 the history list is called a history event and is assigned a number,
48 beginning with 1 (one) when the shell starts up. The history number
49 that you may see in your prompt (see EXPANSION OF PROMPT SEQUENCES in
50 zshmisc(1)) is the number that is to be assigned to the next command.
51
52 Overview
53 A history expansion begins with the first character of the histchars
54 parameter, which is `!' by default, and may occur anywhere on the com‐
55 mand line; history expansions do not nest. The `!' can be escaped with
56 `\' or can be enclosed between a pair of single quotes ('') to suppress
57 its special meaning. Double quotes will not work for this. Following
58 this history character is an optional event designator (see the section
59 `Event Designators') and then an optional word designator (the section
60 `Word Designators'); if neither of these designators is present, no
61 history expansion occurs.
62
63 Input lines containing history expansions are echoed after being
64 expanded, but before any other expansions take place and before the
65 command is executed. It is this expanded form that is recorded as the
66 history event for later references.
67
68 By default, a history reference with no event designator refers to the
69 same event as any preceding history reference on that command line; if
70 it is the only history reference in a command, it refers to the previ‐
71 ous command. However, if the option CSH_JUNKIE_HISTORY is set, then
72 every history reference with no event specification always refers to
73 the previous command.
74
75 For example, `!' is the event designator for the previous command, so
76 `!!:1' always refers to the first word of the previous command, and
77 `!!$' always refers to the last word of the previous command. With
78 CSH_JUNKIE_HISTORY set, then `!:1' and `!$' function in the same manner
79 as `!!:1' and `!!$', respectively. Conversely, if CSH_JUNKIE_HISTORY
80 is unset, then `!:1' and `!$' refer to the first and last words,
81 respectively, of the same event referenced by the nearest other history
82 reference preceding them on the current command line, or to the previ‐
83 ous command if there is no preceding reference.
84
85 The character sequence `^foo^bar' (where `^' is actually the second
86 character of the histchars parameter) repeats the last command, replac‐
87 ing the string foo with bar. More precisely, the sequence `^foo^bar^'
88 is synonymous with `!!:s^foo^bar^', hence other modifiers (see the sec‐
89 tion `Modifiers') may follow the final `^'. In particular,
90 `^foo^bar^:G' performs a global substitution.
91
92 If the shell encounters the character sequence `!"' in the input, the
93 history mechanism is temporarily disabled until the current list (see
94 zshmisc(1)) is fully parsed. The `!"' is removed from the input, and
95 any subsequent `!' characters have no special significance.
96
97 A less convenient but more comprehensible form of command history sup‐
98 port is provided by the fc builtin.
99
100 Event Designators
101 An event designator is a reference to a command-line entry in the his‐
102 tory list. In the list below, remember that the initial `!' in each
103 item may be changed to another character by setting the histchars
104 parameter.
105
106 ! Start a history expansion, except when followed by a blank, new‐
107 line, `=' or `('. If followed immediately by a word designator
108 (see the section `Word Designators'), this forms a history ref‐
109 erence with no event designator (see the section `Overview').
110
111 !! Refer to the previous command. By itself, this expansion
112 repeats the previous command.
113
114 !n Refer to command-line n.
115
116 !-n Refer to the current command-line minus n.
117
118 !str Refer to the most recent command starting with str.
119
120 !?str[?]
121 Refer to the most recent command containing str. The trailing
122 `?' is necessary if this reference is to be followed by a modi‐
123 fier or followed by any text that is not to be considered part
124 of str.
125
126 !# Refer to the current command line typed in so far. The line is
127 treated as if it were complete up to and including the word
128 before the one with the `!#' reference.
129
130 !{...} Insulate a history reference from adjacent characters (if neces‐
131 sary).
132
133 Word Designators
134 A word designator indicates which word or words of a given command line
135 are to be included in a history reference. A `:' usually separates the
136 event specification from the word designator. It may be omitted only
137 if the word designator begins with a `^', `$', `*', `-' or `%'. Word
138 designators include:
139
140 0 The first input word (command).
141 n The nth argument.
142 ^ The first argument. That is, 1.
143 $ The last argument.
144 % The word matched by (the most recent) ?str search.
145 x-y A range of words; x defaults to 0.
146 * All the arguments, or a null value if there are none.
147 x* Abbreviates `x-$'.
148 x- Like `x*' but omitting word $.
149
150 Note that a `%' word designator works only when used in one of `!%',
151 `!:%' or `!?str?:%', and only when used after a !? expansion (possibly
152 in an earlier command). Anything else results in an error, although
153 the error may not be the most obvious one.
154
155 Modifiers
156 After the optional word designator, you can add a sequence of one or
157 more of the following modifiers, each preceded by a `:'. These modi‐
158 fiers also work on the result of filename generation and parameter
159 expansion, except where noted.
160
161 a Turn a file name into an absolute path: prepends the current
162 directory, if necessary, and resolves any use of `..' and `.' in
163 the path. Note that the transformation takes place even if the
164 file or any intervening directories do not exist.
165
166 A As `a', but also resolve use of symbolic links where possible.
167 Note that resolution of `..' occurs before resolution of sym‐
168 bolic links. This call is equivalent to a unless your system
169 has the realpath system call (modern systems do).
170
171 c Resolve a command name into an absolute path by searching the
172 command path given by the PATH variable. This does not work for
173 commands containing directory parts. Note also that this does
174 not usually work as a glob qualifier unless a file of the same
175 name is found in the current directory.
176
177 e Remove all but the part of the filename extension following the
178 `.'; see the definition of the filename extension in the
179 description of the r modifier below. Note that according to
180 that definition the result will be empty if the string ends with
181 a `.'.
182
183 h Remove a trailing pathname component, leaving the head. This
184 works like `dirname'.
185
186 l Convert the words to all lowercase.
187
188 p Print the new command but do not execute it. Only works with
189 history expansion.
190
191 q Quote the substituted words, escaping further substitutions.
192 Works with history expansion and parameter expansion, though for
193 parameters it is only useful if the resulting text is to be
194 re-evaluated such as by eval.
195
196 Q Remove one level of quotes from the substituted words.
197
198 r Remove a filename extension leaving the root name. Strings with
199 no filename extension are not altered. A filename extension is
200 a `.' followed by any number of characters (including zero) that
201 are neither `.' nor `/' and that continue to the end of the
202 string. For example, the extension of `foo.orig.c' is `.c', and
203 `dir.c/foo' has no extension.
204
205 s/l/r[/]
206 Substitute r for l as described below. The substitution is done
207 only for the first string that matches l. For arrays and for
208 filename generation, this applies to each word of the expanded
209 text. See below for further notes on substitutions.
210
211 The forms `gs/l/r' and `s/l/r/:G' perform global substitution,
212 i.e. substitute every occurrence of r for l. Note that the g or
213 :G must appear in exactly the position shown.
214
215 See further notes on this form of substitution below.
216
217 & Repeat the previous s substitution. Like s, may be preceded
218 immediately by a g. In parameter expansion the & must appear
219 inside braces, and in filename generation it must be quoted with
220 a backslash.
221
222 t Remove all leading pathname components, leaving the tail. This
223 works like `basename'.
224
225 u Convert the words to all uppercase.
226
227 x Like q, but break into words at whitespace. Does not work with
228 parameter expansion.
229
230 The s/l/r/ substitution works as follows. By default the left-hand
231 side of substitutions are not patterns, but character strings. Any
232 character can be used as the delimiter in place of `/'. A backslash
233 quotes the delimiter character. The character `&', in the
234 right-hand-side r, is replaced by the text from the left-hand-side l.
235 The `&' can be quoted with a backslash. A null l uses the previous
236 string either from the previous l or from the contextual scan string s
237 from `!?s'. You can omit the rightmost delimiter if a newline immedi‐
238 ately follows r; the rightmost `?' in a context scan can similarly be
239 omitted. Note the same record of the last l and r is maintained across
240 all forms of expansion.
241
242 Note that if a `&' is used within glob qualifers an extra backslash is
243 needed as a & is a special character in this case.
244
245 If the option HIST_SUBST_PATTERN is set, l is treated as a pattern of
246 the usual form described in the section FILENAME GENERATION below.
247 This can be used in all the places where modifiers are available; note,
248 however, that in globbing qualifiers parameter substitution has already
249 taken place, so parameters in the replacement string should be quoted
250 to ensure they are replaced at the correct time. Note also that com‐
251 plicated patterns used in globbing qualifiers may need the extended
252 glob qualifier notation (#q:s/.../.../) in order for the shell to rec‐
253 ognize the expression as a glob qualifier. Further, note that bad pat‐
254 terns in the substitution are not subject to the NO_BAD_PATTERN option
255 so will cause an error.
256
257 When HIST_SUBST_PATTERN is set, l may start with a # to indicate that
258 the pattern must match at the start of the string to be substituted,
259 and a % may appear at the start or after an # to indicate that the pat‐
260 tern must match at the end of the string to be substituted. The % or #
261 may be quoted with two backslashes.
262
263 For example, the following piece of filename generation code with the
264 EXTENDED_GLOB option:
265
266 print *.c(#q:s/#%(#b)s(*).c/'S${match[1]}.C'/)
267
268 takes the expansion of *.c and applies the glob qualifiers in the
269 (#q...) expression, which consists of a substitution modifier anchored
270 to the start and end of each word (#%). This turns on backreferences
271 ((#b)), so that the parenthesised subexpression is available in the
272 replacement string as ${match[1]}. The replacement string is quoted so
273 that the parameter is not substituted before the start of filename gen‐
274 eration.
275
276 The following f, F, w and W modifiers work only with parameter expan‐
277 sion and filename generation. They are listed here to provide a single
278 point of reference for all modifiers.
279
280 f Repeats the immediately (without a colon) following modifier
281 until the resulting word doesn't change any more.
282
283 F:expr:
284 Like f, but repeats only n times if the expression expr evalu‐
285 ates to n. Any character can be used instead of the `:'; if
286 `(', `[', or `{' is used as the opening delimiter, the closing
287 delimiter should be ')', `]', or `}', respectively.
288
289 w Makes the immediately following modifier work on each word in
290 the string.
291
292 W:sep: Like w but words are considered to be the parts of the string
293 that are separated by sep. Any character can be used instead of
294 the `:'; opening parentheses are handled specially, see above.
295
297 Each part of a command argument that takes the form `<(list)',
298 `>(list)' or `=(list)' is subject to process substitution. The expres‐
299 sion may be preceded or followed by other strings except that, to pre‐
300 vent clashes with commonly occurring strings and patterns, the last
301 form must occur at the start of a command argument, and the forms are
302 only expanded when first parsing command or assignment arguments.
303 Process substitutions may be used following redirection operators; in
304 this case, the substitution must appear with no trailing string.
305
306 In the case of the < or > forms, the shell runs the commands in list as
307 a subprocess of the job executing the shell command line. If the sys‐
308 tem supports the /dev/fd mechanism, the command argument is the name of
309 the device file corresponding to a file descriptor; otherwise, if the
310 system supports named pipes (FIFOs), the command argument will be a
311 named pipe. If the form with > is selected then writing on this spe‐
312 cial file will provide input for list. If < is used, then the file
313 passed as an argument will be connected to the output of the list
314 process. For example,
315
316 paste <(cut -f1 file1) <(cut -f3 file2) |
317 tee >(process1) >(process2) >/dev/null
318
319 cuts fields 1 and 3 from the files file1 and file2 respectively, pastes
320 the results together, and sends it to the processes process1 and
321 process2.
322
323 If =(...) is used instead of <(...), then the file passed as an argu‐
324 ment will be the name of a temporary file containing the output of the
325 list process. This may be used instead of the < form for a program
326 that expects to lseek (see lseek(2)) on the input file.
327
328 There is an optimisation for substitutions of the form =(<<<arg), where
329 arg is a single-word argument to the here-string redirection <<<. This
330 form produces a file name containing the value of arg after any substi‐
331 tutions have been performed. This is handled entirely within the cur‐
332 rent shell. This is effectively the reverse of the special form
333 $(<arg) which treats arg as a file name and replaces it with the file's
334 contents.
335
336 The = form is useful as both the /dev/fd and the named pipe implementa‐
337 tion of <(...) have drawbacks. In the former case, some programmes may
338 automatically close the file descriptor in question before examining
339 the file on the command line, particularly if this is necessary for
340 security reasons such as when the programme is running setuid. In the
341 second case, if the programme does not actually open the file, the sub‐
342 shell attempting to read from or write to the pipe will (in a typical
343 implementation, different operating systems may have different behav‐
344 iour) block for ever and have to be killed explicitly. In both cases,
345 the shell actually supplies the information using a pipe, so that pro‐
346 grammes that expect to lseek (see lseek(2)) on the file will not work.
347
348 Also note that the previous example can be more compactly and effi‐
349 ciently written (provided the MULTIOS option is set) as:
350
351 paste <(cut -f1 file1) <(cut -f3 file2) \
352 > >(process1) > >(process2)
353
354 The shell uses pipes instead of FIFOs to implement the latter two
355 process substitutions in the above example.
356
357 There is an additional problem with >(process); when this is attached
358 to an external command, the parent shell does not wait for process to
359 finish and hence an immediately following command cannot rely on the
360 results being complete. The problem and solution are the same as
361 described in the section MULTIOS in zshmisc(1). Hence in a simplified
362 version of the example above:
363
364 paste <(cut -f1 file1) <(cut -f3 file2) > >(process)
365
366 (note that no MULTIOS are involved), process will be run asynchronously
367 as far as the parent shell is concerned. The workaround is:
368
369 { paste <(cut -f1 file1) <(cut -f3 file2) } > >(process)
370
371 The extra processes here are spawned from the parent shell which will
372 wait for their completion.
373
374 Another problem arises any time a job with a substitution that requires
375 a temporary file is disowned by the shell, including the case where
376 `&!' or `&|' appears at the end of a command containing a subsitution.
377 In that case the temporary file will not be cleaned up as the shell no
378 longer has any memory of the job. A workaround is to use a subshell,
379 for example,
380
381 (mycmd =(myoutput)) &!
382
383 as the forked subshell will wait for the command to finish then remove
384 the temporary file.
385
386 A general workaround to ensure a process substitution endures for an
387 appropriate length of time is to pass it as a parameter to an anonymous
388 shell function (a piece of shell code that is run immediately with
389 function scope). For example, this code:
390
391 () {
392 print File $1:
393 cat $1
394 } =(print This be the verse)
395
396 outputs something resembling the following
397
398 File /tmp/zsh6nU0kS:
399 This be the verse
400
401 The temporary file created by the process substitution will be deleted
402 when the function exits.
403
405 The character `$' is used to introduce parameter expansions. See zsh‐
406 param(1) for a description of parameters, including arrays, associative
407 arrays, and subscript notation to access individual array elements.
408
409 Note in particular the fact that words of unquoted parameters are not
410 automatically split on whitespace unless the option SH_WORD_SPLIT is
411 set; see references to this option below for more details. This is an
412 important difference from other shells.
413
414 In the expansions discussed below that require a pattern, the form of
415 the pattern is the same as that used for filename generation; see the
416 section `Filename Generation'. Note that these patterns, along with
417 the replacement text of any substitutions, are themselves subject to
418 parameter expansion, command substitution, and arithmetic expansion.
419 In addition to the following operations, the colon modifiers described
420 in the section `Modifiers' in the section `History Expansion' can be
421 applied: for example, ${i:s/foo/bar/} performs string substitution on
422 the expansion of parameter $i.
423
424 ${name}
425 The value, if any, of the parameter name is substituted. The
426 braces are required if the expansion is to be followed by a let‐
427 ter, digit, or underscore that is not to be interpreted as part
428 of name. In addition, more complicated forms of substitution
429 usually require the braces to be present; exceptions, which only
430 apply if the option KSH_ARRAYS is not set, are a single sub‐
431 script or any colon modifiers appearing after the name, or any
432 of the characters `^', `=', `~', `#' or `+' appearing before the
433 name, all of which work with or without braces.
434
435 If name is an array parameter, and the KSH_ARRAYS option is not
436 set, then the value of each element of name is substituted, one
437 element per word. Otherwise, the expansion results in one word
438 only; with KSH_ARRAYS, this is the first element of an array.
439 No field splitting is done on the result unless the
440 SH_WORD_SPLIT option is set. See also the flags = and
441 s:string:.
442
443 ${+name}
444 If name is the name of a set parameter `1' is substituted, oth‐
445 erwise `0' is substituted.
446
447 ${name-word}
448 ${name:-word}
449 If name is set, or in the second form is non-null, then substi‐
450 tute its value; otherwise substitute word. In the second form
451 name may be omitted, in which case word is always substituted.
452
453 ${name+word}
454 ${name:+word}
455 If name is set, or in the second form is non-null, then substi‐
456 tute word; otherwise substitute nothing.
457
458 ${name=word}
459 ${name:=word}
460 ${name::=word}
461 In the first form, if name is unset then set it to word; in the
462 second form, if name is unset or null then set it to word; and
463 in the third form, unconditionally set name to word. In all
464 forms, the value of the parameter is then substituted.
465
466 ${name?word}
467 ${name:?word}
468 In the first form, if name is set, or in the second form if name
469 is both set and non-null, then substitute its value; otherwise,
470 print word and exit from the shell. Interactive shells instead
471 return to the prompt. If word is omitted, then a standard mes‐
472 sage is printed.
473
474 In any of the above expressions that test a variable and substitute an
475 alternate word, note that you can use standard shell quoting in the
476 word value to selectively override the splitting done by the
477 SH_WORD_SPLIT option and the = flag, but not splitting by the s:string:
478 flag.
479
480 In the following expressions, when name is an array and the substitu‐
481 tion is not quoted, or if the `(@)' flag or the name[@] syntax is used,
482 matching and replacement is performed on each array element separately.
483
484 ${name#pattern}
485 ${name##pattern}
486 If the pattern matches the beginning of the value of name, then
487 substitute the value of name with the matched portion deleted;
488 otherwise, just substitute the value of name. In the first
489 form, the smallest matching pattern is preferred; in the second
490 form, the largest matching pattern is preferred.
491
492 ${name%pattern}
493 ${name%%pattern}
494 If the pattern matches the end of the value of name, then sub‐
495 stitute the value of name with the matched portion deleted; oth‐
496 erwise, just substitute the value of name. In the first form,
497 the smallest matching pattern is preferred; in the second form,
498 the largest matching pattern is preferred.
499
500 ${name:#pattern}
501 If the pattern matches the value of name, then substitute the
502 empty string; otherwise, just substitute the value of name. If
503 name is an array the matching array elements are removed (use
504 the `(M)' flag to remove the non-matched elements).
505
506 ${name:|arrayname}
507 If arrayname is the name (N.B., not contents) of an array vari‐
508 able, then any elements contained in arrayname are removed from
509 the substitution of name. If the substitution is scalar, either
510 because name is a scalar variable or the expression is quoted,
511 the elements of arrayname are instead tested against the entire
512 expression.
513
514 ${name:*arrayname}
515 Similar to the preceding subsitution, but in the opposite sense,
516 so that entries present in both the original substitution and as
517 elements of arrayname are retained and others removed.
518
519 ${name:offset}
520 ${name:offset:length}
521 This syntax gives effects similar to parameter subscripting in
522 the form $name[start,end], but is compatible with other shells;
523 note that both offset and length are interpreted differently
524 from the components of a subscript.
525
526 If offset is non-negative, then if the variable name is a scalar
527 substitute the contents starting offset characters from the
528 first character of the string, and if name is an array substi‐
529 tute elements starting offset elements from the first element.
530 If length is given, substitute that many characters or elements,
531 otherwise the entire rest of the scalar or array.
532
533 A positive offset is always treated as the offset of a character
534 or element in name from the first character or element of the
535 array (this is different from native zsh subscript notation).
536 Hence 0 refers to the first character or element regardless of
537 the setting of the option KSH_ARRAYS.
538
539 A negative offset counts backwards from the end of the scalar or
540 array, so that -1 corresponds to the last character or element,
541 and so on.
542
543 When positive, length counts from the offset position toward the
544 end of the scalar or array. When negative, length counts back
545 from the end. If this results in a position smaller than off‐
546 set, a diagnostic is printed and nothing is substituted.
547
548 The option MULTIBYTE is obeyed, i.e. the offset and length count
549 multibyte characters where appropriate.
550
551 offset and length undergo the same set of shell substitutions as
552 for scalar assignment; in addition, they are then subject to
553 arithmetic evaluation. Hence, for example
554
555 print ${foo:3}
556 print ${foo: 1 + 2}
557 print ${foo:$(( 1 + 2))}
558 print ${foo:$(echo 1 + 2)}
559
560 all have the same effect, extracting the string starting at the
561 fourth character of $foo if the substution would otherwise
562 return a scalar, or the array starting at the fourth element if
563 $foo would return an array. Note that with the option
564 KSH_ARRAYS $foo always returns a scalar (regardless of the use
565 of the offset syntax) and a form such as $foo[*]:3 is required
566 to extract elements of an array named foo.
567
568 If offset is negative, the - may not appear immediately after
569 the : as this indicates the ${name:-word} form of substitution.
570 Instead, a space may be inserted before the -. Furthermore,
571 neither offset nor length may begin with an alphabetic character
572 or & as these are used to indicate history-style modifiers. To
573 substitute a value from a variable, the recommended approach is
574 to precede it with a $ as this signifies the intention (parame‐
575 ter substitution can easily be rendered unreadable); however, as
576 arithmetic substitution is performed, the expression ${var:
577 offs} does work, retrieving the offset from $offs.
578
579 For further compatibility with other shells there is a special
580 case for array offset 0. This usually accesses to the first
581 element of the array. However, if the substitution refers the
582 positional parameter array, e.g. $@ or $*, then offset 0 instead
583 refers to $0, offset 1 refers to $1, and so on. In other words,
584 the positional parameter array is effectively extended by
585 prepending $0. Hence ${*:0:1} substitutes $0 and ${*:1:1} sub‐
586 stitutes $1.
587
588 ${name/pattern/repl}
589 ${name//pattern/repl}
590 Replace the longest possible match of pattern in the expansion
591 of parameter name by string repl. The first form replaces just
592 the first occurrence, the second form all occurrences. Both
593 pattern and repl are subject to double-quoted substitution, so
594 that expressions like ${name/$opat/$npat} will work, but note
595 the usual rule that pattern characters in $opat are not treated
596 specially unless either the option GLOB_SUBST is set, or $opat
597 is instead substituted as ${~opat}.
598
599 The pattern may begin with a `#', in which case the pattern must
600 match at the start of the string, or `%', in which case it must
601 match at the end of the string, or `#%' in which case the pat‐
602 tern must match the entire string. The repl may be an empty
603 string, in which case the final `/' may also be omitted. To
604 quote the final `/' in other cases it should be preceded by a
605 single backslash; this is not necessary if the `/' occurs inside
606 a substituted parameter. Note also that the `#', `%' and `#%
607 are not active if they occur inside a substituted parameter,
608 even at the start.
609
610 The first `/' may be preceded by a `:', in which case the match
611 will only succeed if it matches the entire word. Note also the
612 effect of the I and S parameter expansion flags below; however,
613 the flags M, R, B, E and N are not useful.
614
615 For example,
616
617 foo="twinkle twinkle little star" sub="t*e" rep="spy"
618 print ${foo//${~sub}/$rep}
619 print ${(S)foo//${~sub}/$rep}
620
621 Here, the `~' ensures that the text of $sub is treated as a pat‐
622 tern rather than a plain string. In the first case, the longest
623 match for t*e is substituted and the result is `spy star', while
624 in the second case, the shortest matches are taken and the
625 result is `spy spy lispy star'.
626
627 ${#spec}
628 If spec is one of the above substitutions, substitute the length
629 in characters of the result instead of the result itself. If
630 spec is an array expression, substitute the number of elements
631 of the result. Note that `^', `=', and `~', below, must appear
632 to the left of `#' when these forms are combined.
633
634 ${^spec}
635 Turn on the RC_EXPAND_PARAM option for the evaluation of spec;
636 if the `^' is doubled, turn it off. When this option is set,
637 array expansions of the form foo${xx}bar, where the parameter xx
638 is set to (a b c), are substituted with `fooabar foobbar
639 foocbar' instead of the default `fooa b cbar'. Note that an
640 empty array will therefore cause all arguments to be removed.
641
642 Internally, each such expansion is converted into the equivalent
643 list for brace expansion. E.g., ${^var} becomes
644 {$var[1],$var[2],...}, and is processed as described in the sec‐
645 tion `Brace Expansion' below. If word splitting is also in
646 effect the $var[N] may themselves be split into different list
647 elements.
648
649 ${=spec}
650 Perform word splitting using the rules for SH_WORD_SPLIT during
651 the evaluation of spec, but regardless of whether the parameter
652 appears in double quotes; if the `=' is doubled, turn it off.
653 This forces parameter expansions to be split into separate words
654 before substitution, using IFS as a delimiter. This is done by
655 default in most other shells.
656
657 Note that splitting is applied to word in the assignment forms
658 of spec before the assignment to name is performed. This
659 affects the result of array assignments with the A flag.
660
661 ${~spec}
662 Turn on the GLOB_SUBST option for the evaluation of spec; if the
663 `~' is doubled, turn it off. When this option is set, the
664 string resulting from the expansion will be interpreted as a
665 pattern anywhere that is possible, such as in filename expansion
666 and filename generation and pattern-matching contexts like the
667 right hand side of the `=' and `!=' operators in conditions.
668
669 In nested substitutions, note that the effect of the ~ applies
670 to the result of the current level of substitution. A surround‐
671 ing pattern operation on the result may cancel it. Hence, for
672 example, if the parameter foo is set to *, ${~foo//\*/*.c} is
673 substituted by the pattern *.c, which may be expanded by file‐
674 name generation, but ${${~foo}//\*/*.c} substitutes to the
675 string *.c, which will not be further expanded.
676
677 If a ${...} type parameter expression or a $(...) type command substi‐
678 tution is used in place of name above, it is expanded first and the
679 result is used as if it were the value of name. Thus it is possible to
680 perform nested operations: ${${foo#head}%tail} substitutes the value
681 of $foo with both `head' and `tail' deleted. The form with $(...) is
682 often useful in combination with the flags described next; see the
683 examples below. Each name or nested ${...} in a parameter expansion
684 may also be followed by a subscript expression as described in Array
685 Parameters in zshparam(1).
686
687 Note that double quotes may appear around nested expressions, in which
688 case only the part inside is treated as quoted; for example,
689 ${(f)"$(foo)"} quotes the result of $(foo), but the flag `(f)' (see
690 below) is applied using the rules for unquoted expansions. Note fur‐
691 ther that quotes are themselves nested in this context; for example, in
692 "${(@f)"$(foo)"}", there are two sets of quotes, one surrounding the
693 whole expression, the other (redundant) surrounding the $(foo) as
694 before.
695
696 Parameter Expansion Flags
697 If the opening brace is directly followed by an opening parenthesis,
698 the string up to the matching closing parenthesis will be taken as a
699 list of flags. In cases where repeating a flag is meaningful, the rep‐
700 etitions need not be consecutive; for example, `(q%q%q)' means the same
701 thing as the more readable `(%%qqq)'. The following flags are sup‐
702 ported:
703
704 # Evaluate the resulting words as numeric expressions and output
705 the characters corresponding to the resulting integer. Note
706 that this form is entirely distinct from use of the # without
707 parentheses.
708
709 If the MULTIBYTE option is set and the number is greater than
710 127 (i.e. not an ASCII character) it is treated as a Unicode
711 character.
712
713 % Expand all % escapes in the resulting words in the same way as
714 in prompts (see EXPANSION OF PROMPT SEQUENCES in zshmisc(1)). If
715 this flag is given twice, full prompt expansion is done on the
716 resulting words, depending on the setting of the PROMPT_PERCENT,
717 PROMPT_SUBST and PROMPT_BANG options.
718
719 @ In double quotes, array elements are put into separate words.
720 E.g., `"${(@)foo}"' is equivalent to `"${foo[@]}"' and
721 `"${(@)foo[1,2]}"' is the same as `"$foo[1]" "$foo[2]"'. This
722 is distinct from field splitting by the f, s or z flags, which
723 still applies within each array element.
724
725 A Create an array parameter with `${...=...}', `${...:=...}' or
726 `${...::=...}'. If this flag is repeated (as in `AA'), create
727 an associative array parameter. Assignment is made before sort‐
728 ing or padding. The name part may be a subscripted range for
729 ordinary arrays; the word part must be converted to an array,
730 for example by using `${(AA)=name=...}' to activate field split‐
731 ting, when creating an associative array.
732
733 a Sort in array index order; when combined with `O' sort in
734 reverse array index order. Note that `a' is therefore equiva‐
735 lent to the default but `Oa' is useful for obtaining an array's
736 elements in reverse order.
737
738 c With ${#name}, count the total number of characters in an array,
739 as if the elements were concatenated with spaces between them.
740
741 C Capitalize the resulting words. `Words' in this case refers to
742 sequences of alphanumeric characters separated by non-alphanu‐
743 merics, not to words that result from field splitting.
744
745 D Assume the string or array elements contain directories and
746 attempt to substitute the leading part of these by names. The
747 remainder of the path (the whole of it if the leading part was
748 not subsituted) is then quoted so that the whole string can be
749 used as a shell argument. This is the reverse of `~' substitu‐
750 tion: see the section FILENAME EXPANSION below.
751
752 e Perform parameter expansion, command substitution and arithmetic
753 expansion on the result. Such expansions can be nested but too
754 deep recursion may have unpredictable effects.
755
756 f Split the result of the expansion at newlines. This is a short‐
757 hand for `ps:\n:'.
758
759 F Join the words of arrays together using newline as a separator.
760 This is a shorthand for `pj:\n:'.
761
762 g:opts:
763 Process escape sequences like the echo builtin when no options
764 are given (g::). With the o option, octal escapes don't take a
765 leading zero. With the c option, sequences like `^X' are also
766 processed. With the e option, processes `\M-t' and similar
767 sequences like the print builtin. With both of the o and e
768 options, behaves like the print builtin except that in none of
769 these modes is `\c' interpreted.
770
771 i Sort case-insensitively. May be combined with `n' or `O'.
772
773 k If name refers to an associative array, substitute the keys
774 (element names) rather than the values of the elements. Used
775 with subscripts (including ordinary arrays), force indices or
776 keys to be substituted even if the subscript form refers to val‐
777 ues. However, this flag may not be combined with subscript
778 ranges.
779
780 L Convert all letters in the result to lower case.
781
782 n Sort decimal integers numerically; if the first differing char‐
783 acters of two test strings are not digits, sorting is lexical.
784 Integers with more initial zeroes are sorted before those with
785 fewer or none. Hence the array `foo1 foo02 foo2 foo3 foo20
786 foo23' is sorted into the order shown. May be combined with `i'
787 or `O'.
788
789 o Sort the resulting words in ascending order; if this appears on
790 its own the sorting is lexical and case-sensitive (unless the
791 locale renders it case-insensitive). Sorting in ascending order
792 is the default for other forms of sorting, so this is ignored if
793 combined with `a', `i' or `n'.
794
795 O Sort the resulting words in descending order; `O' without `a',
796 `i' or `n' sorts in reverse lexical order. May be combined with
797 `a', `i' or `n' to reverse the order of sorting.
798
799 P This forces the value of the parameter name to be interpreted as
800 a further parameter name, whose value will be used where appro‐
801 priate. Note that flags set with one of the typeset family of
802 commands (in particular case transformations) are not applied to
803 the value of name used in this fashion.
804
805 If used with a nested parameter or command substitution, the
806 result of that will be taken as a parameter name in the same
807 way. For example, if you have `foo=bar' and `bar=baz', the
808 strings ${(P)foo}, ${(P)${foo}}, and ${(P)$(echo bar)} will be
809 expanded to `baz'.
810
811 q Quote characters that are special to the shell in the resulting
812 words with backslashes; unprintable or invalid characters are
813 quoted using the $'\NNN' form, with separate quotes for each
814 octet.
815
816 If this flag is given twice, the resulting words are quoted in
817 single quotes and if it is given three times, the words are
818 quoted in double quotes; in these forms no special handling of
819 unprintable or invalid characters is attempted. If the flag is
820 given four times, the words are quoted in single quotes preceded
821 by a $. Note that in all three of these forms quoting is done
822 unconditionally, even if this does not change the way the
823 resulting string would be interpreted by the shell.
824
825 If a q- is given (only a single q may appear), a minimal form of
826 single quoting is used that only quotes the string if needed to
827 protect special characters. Typically this form gives the most
828 readable output.
829
830 Q Remove one level of quotes from the resulting words.
831
832 t Use a string describing the type of the parameter where the
833 value of the parameter would usually appear. This string con‐
834 sists of keywords separated by hyphens (`-'). The first keyword
835 in the string describes the main type, it can be one of
836 `scalar', `array', `integer', `float' or `association'. The
837 other keywords describe the type in more detail:
838
839 local for local parameters
840
841 left for left justified parameters
842
843 right_blanks
844 for right justified parameters with leading blanks
845
846 right_zeros
847 for right justified parameters with leading zeros
848
849 lower for parameters whose value is converted to all lower case
850 when it is expanded
851
852 upper for parameters whose value is converted to all upper case
853 when it is expanded
854
855 readonly
856 for readonly parameters
857
858 tag for tagged parameters
859
860 export for exported parameters
861
862 unique for arrays which keep only the first occurrence of dupli‐
863 cated values
864
865 hide for parameters with the `hide' flag
866
867 special
868 for special parameters defined by the shell
869
870 u Expand only the first occurrence of each unique word.
871
872 U Convert all letters in the result to upper case.
873
874 v Used with k, substitute (as two consecutive words) both the key
875 and the value of each associative array element. Used with sub‐
876 scripts, force values to be substituted even if the subscript
877 form refers to indices or keys.
878
879 V Make any special characters in the resulting words visible.
880
881 w With ${#name}, count words in arrays or strings; the s flag may
882 be used to set a word delimiter.
883
884 W Similar to w with the difference that empty words between
885 repeated delimiters are also counted.
886
887 X With this flag, parsing errors occurring with the Q, e and #
888 flags or the pattern matching forms such as `${name#pattern}'
889 are reported. Without the flag, errors are silently ignored.
890
891 z Split the result of the expansion into words using shell parsing
892 to find the words, i.e. taking into account any quoting in the
893 value. Comments are not treated specially but as ordinary
894 strings, similar to interactive shells with the INTERACTIVE_COM‐
895 MENTS option unset (however, see the Z flag below for related
896 options)
897
898 Note that this is done very late, even later than the `(s)'
899 flag. So to access single words in the result use nested expan‐
900 sions as in `${${(z)foo}[2]}'. Likewise, to remove the quotes in
901 the resulting words use `${(Q)${(z)foo}}'.
902
903 0 Split the result of the expansion on null bytes. This is a
904 shorthand for `ps:\0:'.
905
906 The following flags (except p) are followed by one or more arguments as
907 shown. Any character, or the matching pairs `(...)', `{...}', `[...]',
908 or `<...>', may be used in place of a colon as delimiters, but note
909 that when a flag takes more than one argument, a matched pair of delim‐
910 iters must surround each argument.
911
912 p Recognize the same escape sequences as the print builtin in
913 string arguments to any of the flags described below that follow
914 this argument.
915
916 ~ Force string arguments to any of the flags below that follow
917 within the parentheses to be treated as patterns. Compare with
918 a ~ outside parentheses, which forces the entire substituted
919 string to be treated as a pattern. Hence, for example,
920 [[ "?" = ${(~j.|.)array} ]]
921 with the EXTENDED_GLOB option set succeeds if and only if $array con‐
922 tains the string `?' as an element. The argument may be repeated to
923 toggle the behaviour; its effect only lasts to the end of the parenthe‐
924 sised group.
925
926 j:string:
927 Join the words of arrays together using string as a separator.
928 Note that this occurs before field splitting by the s:string:
929 flag or the SH_WORD_SPLIT option.
930
931 l:expr::string1::string2:
932 Pad the resulting words on the left. Each word will be trun‐
933 cated if required and placed in a field expr characters wide.
934
935 The arguments :string1: and :string2: are optional; neither, the
936 first, or both may be given. Note that the same pairs of delim‐
937 iters must be used for each of the three arguments. The space
938 to the left will be filled with string1 (concatenated as often
939 as needed) or spaces if string1 is not given. If both string1
940 and string2 are given, string2 is inserted once directly to the
941 left of each word, truncated if necessary, before string1 is
942 used to produce any remaining padding.
943
944 If the MULTIBYTE option is in effect, the flag m may also be
945 given, in which case widths will be used for the calculation of
946 padding; otherwise individual multibyte characters are treated
947 as occupying one unit of width.
948
949 If the MULTIBYTE option is not in effect, each byte in the
950 string is treated as occupying one unit of width.
951
952 Control characters are always assumed to be one unit wide; this
953 allows the mechanism to be used for generating repetitions of
954 control characters.
955
956 m Only useful together with one of the flags l or r or with the #
957 length operator when the MULTIBYTE option is in effect. Use the
958 character width reported by the system in calculating how much
959 of the string it occupies or the overall length of the string.
960 Most printable characters have a width of one unit, however cer‐
961 tain Asian character sets and certain special effects use wider
962 characters; combining characters have zero width. Non-printable
963 characters are arbitrarily counted as zero width; how they would
964 actually be displayed will vary.
965
966 If the m is repeated, the character either counts zero (if it
967 has zero width), else one. For printable character strings this
968 has the effect of counting the number of glyphs (visibly sepa‐
969 rate characters), except for the case where combining characters
970 themselves have non-zero width (true in certain alphabets).
971
972 r:expr::string1::string2:
973 As l, but pad the words on the right and insert string2 immedi‐
974 ately to the right of the string to be padded.
975
976 Left and right padding may be used together. In this case the
977 strategy is to apply left padding to the first half width of
978 each of the resulting words, and right padding to the second
979 half. If the string to be padded has odd width the extra pad‐
980 ding is applied on the left.
981
982 s:string:
983 Force field splitting at the separator string. Note that a
984 string of two or more characters means that all of them must
985 match in sequence; this differs from the treatment of two or
986 more characters in the IFS parameter. See also the = flag and
987 the SH_WORD_SPLIT option. An empty string may also be given in
988 which case every character will be a separate element.
989
990 For historical reasons, the usual behaviour that empty array
991 elements are retained inside double quotes is disabled for
992 arrays generated by splitting; hence the following:
993
994 line="one::three"
995 print -l "${(s.:.)line}"
996
997 produces two lines of output for one and three and elides the
998 empty field. To override this behaviour, supply the "(@)" flag
999 as well, i.e. "${(@s.:.)line}".
1000
1001 Z:opts:
1002 As z but takes a combination of option letters between a follow‐
1003 ing pair of delimiter characters. With no options the effect is
1004 identical to z. (Z+c+) causes comments to be parsed as a string
1005 and retained; any field in the resulting array beginning with an
1006 unquoted comment character is a comment. (Z+C+) causes comments
1007 to be parsed and removed. The rule for comments is standard:
1008 anything between a word starting with the third character of
1009 $HISTCHARS, default #, up to the next newline is a comment.
1010 (Z+n+) causes unquoted newlines to be treated as ordinary white‐
1011 space, else they are treated as if they are shell code delim‐
1012 iters and converted to semicolons. Options are combined within
1013 the same set of delimiters, e.g. (Z+Cn+).
1014
1015 _:flags:
1016 The underscore (_) flag is reserved for future use. As of this
1017 revision of zsh, there are no valid flags; anything following an
1018 underscore, other than an empty pair of delimiters, is treated
1019 as an error, and the flag itself has no effect.
1020
1021 The following flags are meaningful with the ${...#...} or ${...%...}
1022 forms. The S and I flags may also be used with the ${.../...} forms.
1023
1024 S Search substrings as well as beginnings or ends; with # start
1025 from the beginning and with % start from the end of the string.
1026 With substitution via ${.../...} or ${...//...}, specifies
1027 non-greedy matching, i.e. that the shortest instead of the long‐
1028 est match should be replaced.
1029
1030 I:expr:
1031 Search the exprth match (where expr evaluates to a number).
1032 This only applies when searching for substrings, either with the
1033 S flag, or with ${.../...} (only the exprth match is substi‐
1034 tuted) or ${...//...} (all matches from the exprth on are sub‐
1035 stituted). The default is to take the first match.
1036
1037 The exprth match is counted such that there is either one or
1038 zero matches from each starting position in the string, although
1039 for global substitution matches overlapping previous replace‐
1040 ments are ignored. With the ${...%...} and ${...%%...} forms,
1041 the starting position for the match moves backwards from the end
1042 as the index increases, while with the other forms it moves for‐
1043 ward from the start.
1044
1045 Hence with the string
1046 which switch is the right switch for Ipswich?
1047 substitutions of the form ${(SI:N:)string#w*ch} as N increases
1048 from 1 will match and remove `which', `witch', `witch' and
1049 `wich'; the form using `##' will match and remove `which switch
1050 is the right switch for Ipswich', `witch is the right switch for
1051 Ipswich', `witch for Ipswich' and `wich'. The form using `%'
1052 will remove the same matches as for `#', but in reverse order,
1053 and the form using `%%' will remove the same matches as for `##'
1054 in reverse order.
1055
1056 B Include the index of the beginning of the match in the result.
1057
1058 E Include the index of the end of the match in the result.
1059
1060 M Include the matched portion in the result.
1061
1062 N Include the length of the match in the result.
1063
1064 R Include the unmatched portion in the result (the Rest).
1065
1066 Rules
1067 Here is a summary of the rules for substitution; this assumes that
1068 braces are present around the substitution, i.e. ${...}. Some particu‐
1069 lar examples are given below. Note that the Zsh Development Group
1070 accepts no responsibility for any brain damage which may occur during
1071 the reading of the following rules.
1072
1073 1. Nested substitution
1074 If multiple nested ${...} forms are present, substitution is
1075 performed from the inside outwards. At each level, the substi‐
1076 tution takes account of whether the current value is a scalar or
1077 an array, whether the whole substitution is in double quotes,
1078 and what flags are supplied to the current level of substitu‐
1079 tion, just as if the nested substitution were the outermost.
1080 The flags are not propagated up to enclosing substitutions; the
1081 nested substitution will return either a scalar or an array as
1082 determined by the flags, possibly adjusted for quoting. All the
1083 following steps take place where applicable at all levels of
1084 substitution. Note that, unless the `(P)' flag is present, the
1085 flags and any subscripts apply directly to the value of the
1086 nested substitution; for example, the expansion ${${foo}}
1087 behaves exactly the same as ${foo}.
1088
1089 At each nested level of substitution, the substituted words
1090 undergo all forms of single-word substitution (i.e. not filename
1091 generation), including command substitution, arithmetic expan‐
1092 sion and filename expansion (i.e. leading ~ and =). Thus, for
1093 example, ${${:-=cat}:h} expands to the directory where the cat
1094 program resides. (Explanation: the internal substitution has no
1095 parameter but a default value =cat, which is expanded by file‐
1096 name expansion to a full path; the outer substitution then
1097 applies the modifier :h and takes the directory part of the
1098 path.)
1099
1100 2. Internal parameter flags
1101 Any parameter flags set by one of the typeset family of com‐
1102 mands, in particular the L, R, Z, u and l flags for padding and
1103 capitalization, are applied directly to the parameter value.
1104 Note these flags are options to the command, e.g. `typeset -Z';
1105 they are not the same as the flags used within parameter substi‐
1106 tutions.
1107
1108 3. Parameter subscripting
1109 If the value is a raw parameter reference with a subscript, such
1110 as ${var[3]}, the effect of subscripting is applied directly to
1111 the parameter. Subscripts are evaluated left to right; subse‐
1112 quent subscripts apply to the scalar or array value yielded by
1113 the previous subscript. Thus if var is an array, ${var[1][2]}
1114 is the second character of the first word, but ${var[2,4][2]} is
1115 the entire third word (the second word of the range of words two
1116 through four of the original array). Any number of subscripts
1117 may appear.
1118
1119 4. Parameter name replacement
1120 The effect of any (P) flag, which treats the value so far as a
1121 parameter name and replaces it with the corresponding value, is
1122 applied.
1123
1124 5. Double-quoted joining
1125 If the value after this process is an array, and the substitu‐
1126 tion appears in double quotes, and no (@) flag is present at the
1127 current level, the words of the value are joined with the first
1128 character of the parameter $IFS, by default a space, between
1129 each word (single word arrays are not modified). If the (j)
1130 flag is present, that is used for joining instead of $IFS.
1131
1132 6. Nested subscripting
1133 Any remaining subscripts (i.e. of a nested substitution) are
1134 evaluated at this point, based on whether the value is an array
1135 or a scalar. As with 3., multiple subscripts can appear. Note
1136 that ${foo[2,4][2]} is thus equivalent to ${${foo[2,4]}[2]} and
1137 also to "${${(@)foo[2,4]}[2]}" (the nested substitution returns
1138 an array in both cases), but not to "${${foo[2,4]}[2]}" (the
1139 nested substitution returns a scalar because of the quotes).
1140
1141 7. Modifiers
1142 Any modifiers, as specified by a trailing `#', `%', `/' (possi‐
1143 bly doubled) or by a set of modifiers of the form :... (see the
1144 section `Modifiers' in the section `History Expansion'), are
1145 applied to the words of the value at this level.
1146
1147 8. Character evaluation
1148 Any (#) flag is applied, evaluating the result so far numeri‐
1149 cally as a character.
1150
1151 9. Length
1152 Any initial # modifier, i.e. in the form ${#var}, is used to
1153 evaluate the length of the expression so far.
1154
1155 10. Forced joining
1156 If the `(j)' flag is present, or no `(j)' flag is present but
1157 the string is to be split as given by rule 11., and joining did
1158 not take place at step 5., any words in the value are joined
1159 together using the given string or the first character of $IFS
1160 if none. Note that the `(F)' flag implicitly supplies a string
1161 for joining in this manner.
1162
1163 11. Simple word splitting
1164 If one of the `(s)' or `(f)' flags are present, or the `=' spec‐
1165 ifier was present (e.g. ${=var}), the word is split on occur‐
1166 rences of the specified string, or (for = with neither of the
1167 two flags present) any of the characters in $IFS.
1168
1169 If no `(s)', `(f)' or `=' was given, but the word is not quoted
1170 and the option SH_WORD_SPLIT is set, the word is split on occur‐
1171 rences of any of the characters in $IFS. Note this step, too,
1172 takes place at all levels of a nested substitution.
1173
1174 12. Case modification
1175 Any case modification from one of the flags (L), (U) or (C) is
1176 applied.
1177
1178 13. Escape sequence replacement
1179 First any replacements from the (g) flag are performed, then any
1180 prompt-style formatting from the (%) family of flags is applied.
1181
1182 14. Quote application
1183 Any quoting or unquoting using (q) and (Q) and related flags is
1184 applied.
1185
1186 15. Directory naming
1187 Any directory name substitution using (D) flag is applied.
1188
1189 16. Visibility enhancement
1190 Any modifications to make characters visible using the (V) flag
1191 are applied.
1192
1193 17. Lexical word splitting
1194 If the '(z)' flag or one of the forms of the '(Z)' flag is
1195 present, the word is split as if it were a shell command line,
1196 so that quotation marks and other metacharacters are used to
1197 decide what constitutes a word. Note this form of splitting is
1198 entirely distinct from that described by rule 11.: it does not
1199 use $IFS, and does not cause forced joining.
1200
1201 18. Uniqueness
1202 If the result is an array and the `(u)' flag was present, dupli‐
1203 cate elements are removed from the array.
1204
1205 19. Ordering
1206 If the result is still an array and one of the `(o)' or `(O)'
1207 flags was present, the array is reordered.
1208
1209 20. Re-evaluation
1210 Any `(e)' flag is applied to the value, forcing it to be
1211 re-examined for new parameter substitutions, but also for com‐
1212 mand and arithmetic substitutions.
1213
1214 21. Padding
1215 Any padding of the value by the `(l.fill.)' or `(r.fill.)' flags
1216 is applied.
1217
1218 22. Semantic joining
1219 In contexts where expansion semantics requires a single word to
1220 result, all words are rejoined with the first character of IFS
1221 between. So in `${(P)${(f)lines}}' the value of ${lines} is
1222 split at newlines, but then must be joined again before the P
1223 flag can be applied.
1224
1225 If a single word is not required, this rule is skipped.
1226
1227 23. Empty argument removal
1228 If the substitution does not appear in double quotes, any
1229 resulting zero-length argument, whether from a scalar or an ele‐
1230 ment of an array, is elided from the list of arguments inserted
1231 into the command line.
1232
1233 Strictly speaking, the removal happens later as the same happens
1234 with other forms of substitution; the point to note here is sim‐
1235 ply that it occurs after any of the above parameter operations.
1236
1237 Examples
1238 The flag f is useful to split a double-quoted substitution line by
1239 line. For example, ${(f)"$(<file)"} substitutes the contents of file
1240 divided so that each line is an element of the resulting array. Com‐
1241 pare this with the effect of $(<file) alone, which divides the file up
1242 by words, or the same inside double quotes, which makes the entire con‐
1243 tent of the file a single string.
1244
1245 The following illustrates the rules for nested parameter expansions.
1246 Suppose that $foo contains the array (bar baz):
1247
1248 "${(@)${foo}[1]}"
1249 This produces the result b. First, the inner substitution
1250 "${foo}", which has no array (@) flag, produces a single word
1251 result "bar baz". The outer substitution "${(@)...[1]}" detects
1252 that this is a scalar, so that (despite the `(@)' flag) the sub‐
1253 script picks the first character.
1254
1255 "${${(@)foo}[1]}"
1256 This produces the result `bar'. In this case, the inner substi‐
1257 tution "${(@)foo}" produces the array `(bar baz)'. The outer
1258 substitution "${...[1]}" detects that this is an array and picks
1259 the first word. This is similar to the simple case "${foo[1]}".
1260
1261 As an example of the rules for word splitting and joining, suppose $foo
1262 contains the array `(ax1 bx1)'. Then
1263
1264 ${(s/x/)foo}
1265 produces the words `a', `1 b' and `1'.
1266
1267 ${(j/x/s/x/)foo}
1268 produces `a', `1', `b' and `1'.
1269
1270 ${(s/x/)foo%%1*}
1271 produces `a' and ` b' (note the extra space). As substitution
1272 occurs before either joining or splitting, the operation first
1273 generates the modified array (ax bx), which is joined to give
1274 "ax bx", and then split to give `a', ` b' and `'. The final
1275 empty string will then be elided, as it is not in double quotes.
1276
1278 A command enclosed in parentheses preceded by a dollar sign, like
1279 `$(...)', or quoted with grave accents, like ``...`', is replaced with
1280 its standard output, with any trailing newlines deleted. If the sub‐
1281 stitution is not enclosed in double quotes, the output is broken into
1282 words using the IFS parameter. The substitution `$(cat foo)' may be
1283 replaced by the equivalent but faster `$(<foo)'. In either case, if
1284 the option GLOB_SUBST is set, the output is eligible for filename gen‐
1285 eration.
1286
1288 A string of the form `$[exp]' or `$((exp))' is substituted with the
1289 value of the arithmetic expression exp. exp is subjected to parameter
1290 expansion, command substitution and arithmetic expansion before it is
1291 evaluated. See the section `Arithmetic Evaluation'.
1292
1294 A string of the form `foo{xx,yy,zz}bar' is expanded to the individual
1295 words `fooxxbar', `fooyybar' and `foozzbar'. Left-to-right order is
1296 preserved. This construct may be nested. Commas may be quoted in
1297 order to include them literally in a word.
1298
1299 An expression of the form `{n1..n2}', where n1 and n2 are integers, is
1300 expanded to every number between n1 and n2 inclusive. If either number
1301 begins with a zero, all the resulting numbers will be padded with lead‐
1302 ing zeroes to that minimum width, but for negative numbers the - char‐
1303 acter is also included in the width. If the numbers are in decreasing
1304 order the resulting sequence will also be in decreasing order.
1305
1306 An expression of the form `{n1..n2..n3}', where n1, n2, and n3 are
1307 integers, is expanded as above, but only every n3th number starting
1308 from n1 is output. If n3 is negative the numbers are output in reverse
1309 order, this is slightly different from simply swapping n1 and n2 in the
1310 case that the step n3 doesn't evenly divide the range. Zero padding
1311 can be specified in any of the three numbers, specifying it in the
1312 third can be useful to pad for example `{-99..100..01}' which is not
1313 possible to specify by putting a 0 on either of the first two numbers
1314 (i.e. pad to two characters).
1315
1316 If a brace expression matches none of the above forms, it is left
1317 unchanged, unless the option BRACE_CCL (an abbreviation for `brace
1318 character class') is set. In that case, it is expanded to a list of
1319 the individual characters between the braces sorted into the order of
1320 the characters in the ASCII character set (multibyte characters are not
1321 currently handled). The syntax is similar to a [...] expression in
1322 filename generation: `-' is treated specially to denote a range of
1323 characters, but `^' or `!' as the first character is treated normally.
1324 For example, `{abcdef0-9}' expands to 16 words 0 1 2 3 4 5 6 7 8 9 a b
1325 c d e f.
1326
1327 Note that brace expansion is not part of filename generation (glob‐
1328 bing); an expression such as */{foo,bar} is split into two separate
1329 words */foo and */bar before filename generation takes place. In par‐
1330 ticular, note that this is liable to produce a `no match' error if
1331 either of the two expressions does not match; this is to be contrasted
1332 with */(foo|bar), which is treated as a single pattern but otherwise
1333 has similar effects.
1334
1335 To combine brace expansion with array expansion, see the ${^spec} form
1336 described in the section Parameter Expansion above.
1337
1339 Each word is checked to see if it begins with an unquoted `~'. If it
1340 does, then the word up to a `/', or the end of the word if there is no
1341 `/', is checked to see if it can be substituted in one of the ways
1342 described here. If so, then the `~' and the checked portion are
1343 replaced with the appropriate substitute value.
1344
1345 A `~' by itself is replaced by the value of $HOME. A `~' followed by a
1346 `+' or a `-' is replaced by current or previous working directory,
1347 respectively.
1348
1349 A `~' followed by a number is replaced by the directory at that posi‐
1350 tion in the directory stack. `~0' is equivalent to `~+', and `~1' is
1351 the top of the stack. `~+' followed by a number is replaced by the
1352 directory at that position in the directory stack. `~+0' is equivalent
1353 to `~+', and `~+1' is the top of the stack. `~-' followed by a number
1354 is replaced by the directory that many positions from the bottom of the
1355 stack. `~-0' is the bottom of the stack. The PUSHD_MINUS option
1356 exchanges the effects of `~+' and `~-' where they are followed by a
1357 number.
1358
1359 Dynamic named directories
1360 If the function zsh_directory_name exists, or the shell variable
1361 zsh_directory_name_functions exists and contains an array of function
1362 names, then the functions are used to implement dynamic directory nam‐
1363 ing. The functions are tried in order until one returns status zero,
1364 so it is important that functions test whether they can handle the case
1365 in question and return an appropriate status.
1366
1367 A `~' followed by a string namstr in unquoted square brackets is
1368 treated specially as a dynamic directory name. Note that the first
1369 unquoted closing square bracket always terminates namstr. The shell
1370 function is passed two arguments: the string n (for name) and namstr.
1371 It should either set the array reply to a single element which is the
1372 directory corresponding to the name and return status zero (executing
1373 an assignment as the last statement is usually sufficient), or it
1374 should return status non-zero. In the former case the element of reply
1375 is used as the directory; in the latter case the substitution is deemed
1376 to have failed. If all functions fail and the option NOMATCH is set,
1377 an error results.
1378
1379 The functions defined as above are also used to see if a directory can
1380 be turned into a name, for example when printing the directory stack or
1381 when expanding %~ in prompts. In this case each function is passed two
1382 arguments: the string d (for directory) and the candidate for dynamic
1383 naming. The function should either return non-zero status, if the
1384 directory cannot be named by the function, or it should set the array
1385 reply to consist of two elements: the first is the dynamic name for the
1386 directory (as would appear within `~[...]'), and the second is the pre‐
1387 fix length of the directory to be replaced. For example, if the trial
1388 directory is /home/myname/src/zsh and the dynamic name for
1389 /home/myname/src (which has 16 characters) is s, then the function sets
1390
1391 reply=(s 16)
1392
1393 The directory name so returned is compared with possible static names
1394 for parts of the directory path, as described below; it is used if the
1395 prefix length matched (16 in the example) is longer than that matched
1396 by any static name.
1397
1398 It is not a requirement that a function implements both n and d calls;
1399 for example, it might be appropriate for certain dynamic forms of
1400 expansion not to be contracted to names. In that case any call with
1401 the first argument d should cause a non-zero status to be returned.
1402
1403 The completion system calls `zsh_directory_name c' followed by equiva‐
1404 lent calls to elements of the array zsh_directory_name_functions, if it
1405 exists, in order to complete dynamic names for directories. The code
1406 for this should be as for any other completion function as described in
1407 zshcompsys(1).
1408
1409 As a working example, here is a function that expands any dynamic names
1410 beginning with the string p: to directories below /home/pws/perforce.
1411 In this simple case a static name for the directory would be just as
1412 effective.
1413
1414 zsh_directory_name() {
1415 emulate -L zsh
1416 setopt extendedglob
1417 local -a match mbegin mend
1418 if [[ $1 = d ]]; then
1419 # turn the directory into a name
1420 if [[ $2 = (#b)(/home/pws/perforce/)([^/]##)* ]]; then
1421 typeset -ga reply
1422 reply=(p:$match[2] $(( ${#match[1]} + ${#match[2]} )) )
1423 else
1424 return 1
1425 fi
1426 elif [[ $1 = n ]]; then
1427 # turn the name into a directory
1428 [[ $2 != (#b)p:(?*) ]] && return 1
1429 typeset -ga reply
1430 reply=(/home/pws/perforce/$match[1])
1431 elif [[ $1 = c ]]; then
1432 # complete names
1433 local expl
1434 local -a dirs
1435 dirs=(/home/pws/perforce/*(/:t))
1436 dirs=(p:${^dirs})
1437 _wanted dynamic-dirs expl 'dynamic directory' compadd -S\] -a dirs
1438 return
1439 else
1440 return 1
1441 fi
1442 return 0
1443 }
1444
1445 Static named directories
1446 A `~' followed by anything not already covered consisting of any number
1447 of alphanumeric characters or underscore (`_'), hyphen (`-'), or dot
1448 (`.') is looked up as a named directory, and replaced by the value of
1449 that named directory if found. Named directories are typically home
1450 directories for users on the system. They may also be defined if the
1451 text after the `~' is the name of a string shell parameter whose value
1452 begins with a `/'. Note that trailing slashes will be removed from the
1453 path to the directory (though the original parameter is not modified).
1454
1455 It is also possible to define directory names using the -d option to
1456 the hash builtin.
1457
1458 In certain circumstances (in prompts, for instance), when the shell
1459 prints a path, the path is checked to see if it has a named directory
1460 as its prefix. If so, then the prefix portion is replaced with a `~'
1461 followed by the name of the directory. The shortest way of referring
1462 to the directory is used, with ties broken in favour of using a named
1463 directory, except when the directory is / itself. The parameters $PWD
1464 and $OLDPWD are never abbreviated in this fashion.
1465
1466 `=' expansion
1467 If a word begins with an unquoted `=' and the EQUALS option is set, the
1468 remainder of the word is taken as the name of a command. If a command
1469 exists by that name, the word is replaced by the full pathname of the
1470 command.
1471
1472 Notes
1473 Filename expansion is performed on the right hand side of a parameter
1474 assignment, including those appearing after commands of the typeset
1475 family. In this case, the right hand side will be treated as a
1476 colon-separated list in the manner of the PATH parameter, so that a `~'
1477 or an `=' following a `:' is eligible for expansion. All such behav‐
1478 iour can be disabled by quoting the `~', the `=', or the whole expres‐
1479 sion (but not simply the colon); the EQUALS option is also respected.
1480
1481 If the option MAGIC_EQUAL_SUBST is set, any unquoted shell argument in
1482 the form `identifier=expression' becomes eligible for file expansion as
1483 described in the previous paragraph. Quoting the first `=' also
1484 inhibits this.
1485
1487 If a word contains an unquoted instance of one of the characters `*',
1488 `(', `|', `<', `[', or `?', it is regarded as a pattern for filename
1489 generation, unless the GLOB option is unset. If the EXTENDED_GLOB
1490 option is set, the `^' and `#' characters also denote a pattern; other‐
1491 wise they are not treated specially by the shell.
1492
1493 The word is replaced with a list of sorted filenames that match the
1494 pattern. If no matching pattern is found, the shell gives an error
1495 message, unless the NULL_GLOB option is set, in which case the word is
1496 deleted; or unless the NOMATCH option is unset, in which case the word
1497 is left unchanged.
1498
1499 In filename generation, the character `/' must be matched explicitly;
1500 also, a `.' must be matched explicitly at the beginning of a pattern or
1501 after a `/', unless the GLOB_DOTS option is set. No filename genera‐
1502 tion pattern matches the files `.' or `..'. In other instances of pat‐
1503 tern matching, the `/' and `.' are not treated specially.
1504
1505 Glob Operators
1506 * Matches any string, including the null string.
1507
1508 ? Matches any character.
1509
1510 [...] Matches any of the enclosed characters. Ranges of characters
1511 can be specified by separating two characters by a `-'. A `-'
1512 or `]' may be matched by including it as the first character in
1513 the list. There are also several named classes of characters,
1514 in the form `[:name:]' with the following meanings. The first
1515 set use the macros provided by the operating system to test for
1516 the given character combinations, including any modifications
1517 due to local language settings, see ctype(3):
1518
1519 [:alnum:]
1520 The character is alphanumeric
1521
1522 [:alpha:]
1523 The character is alphabetic
1524
1525 [:ascii:]
1526 The character is 7-bit, i.e. is a single-byte character
1527 without the top bit set.
1528
1529 [:blank:]
1530 The character is either space or tab
1531
1532 [:cntrl:]
1533 The character is a control character
1534
1535 [:digit:]
1536 The character is a decimal digit
1537
1538 [:graph:]
1539 The character is a printable character other than white‐
1540 space
1541
1542 [:lower:]
1543 The character is a lowercase letter
1544
1545 [:print:]
1546 The character is printable
1547
1548 [:punct:]
1549 The character is printable but neither alphanumeric nor
1550 whitespace
1551
1552 [:space:]
1553 The character is whitespace
1554
1555 [:upper:]
1556 The character is an uppercase letter
1557
1558 [:xdigit:]
1559 The character is a hexadecimal digit
1560
1561 Another set of named classes is handled internally by the shell
1562 and is not sensitive to the locale:
1563
1564 [:IDENT:]
1565 The character is allowed to form part of a shell identi‐
1566 fier, such as a parameter name
1567
1568 [:IFS:]
1569 The character is used as an input field separator, i.e.
1570 is contained in the IFS parameter
1571
1572 [:IFSSPACE:]
1573 The character is an IFS white space character; see the
1574 documentation for IFS in the zshparam(1) manual page.
1575
1576 [:WORD:]
1577 The character is treated as part of a word; this test is
1578 sensitive to the value of the WORDCHARS parameter
1579
1580 Note that the square brackets are additional to those enclosing
1581 the whole set of characters, so to test for a single alphanu‐
1582 meric character you need `[[:alnum:]]'. Named character sets
1583 can be used alongside other types, e.g. `[[:alpha:]0-9]'.
1584
1585 [^...]
1586 [!...] Like [...], except that it matches any character which is not in
1587 the given set.
1588
1589 <[x]-[y]>
1590 Matches any number in the range x to y, inclusive. Either of
1591 the numbers may be omitted to make the range open-ended; hence
1592 `<->' matches any number. To match individual digits, the [...]
1593 form is more efficient.
1594
1595 Be careful when using other wildcards adjacent to patterns of
1596 this form; for example, <0-9>* will actually match any number
1597 whatsoever at the start of the string, since the `<0-9>' will
1598 match the first digit, and the `*' will match any others. This
1599 is a trap for the unwary, but is in fact an inevitable conse‐
1600 quence of the rule that the longest possible match always suc‐
1601 ceeds. Expressions such as `<0-9>[^[:digit:]]*' can be used
1602 instead.
1603
1604 (...) Matches the enclosed pattern. This is used for grouping. If
1605 the KSH_GLOB option is set, then a `@', `*', `+', `?' or `!'
1606 immediately preceding the `(' is treated specially, as detailed
1607 below. The option SH_GLOB prevents bare parentheses from being
1608 used in this way, though the KSH_GLOB option is still available.
1609
1610 Note that grouping cannot extend over multiple directories: it
1611 is an error to have a `/' within a group (this only applies for
1612 patterns used in filename generation). There is one exception:
1613 a group of the form (pat/)# appearing as a complete path segment
1614 can match a sequence of directories. For example, foo/(a*/)#bar
1615 matches foo/bar, foo/any/bar, foo/any/anyother/bar, and so on.
1616
1617 x|y Matches either x or y. This operator has lower precedence than
1618 any other. The `|' character must be within parentheses, to
1619 avoid interpretation as a pipeline.
1620
1621 ^x (Requires EXTENDED_GLOB to be set.) Matches anything except the
1622 pattern x. This has a higher precedence than `/', so `^foo/bar'
1623 will search directories in `.' except `./foo' for a file named
1624 `bar'.
1625
1626 x~y (Requires EXTENDED_GLOB to be set.) Match anything that matches
1627 the pattern x but does not match y. This has lower precedence
1628 than any operator except `|', so `*/*~foo/bar' will search for
1629 all files in all directories in `.' and then exclude `foo/bar'
1630 if there was such a match. Multiple patterns can be excluded by
1631 `foo~bar~baz'. In the exclusion pattern (y), `/' and `.' are
1632 not treated specially the way they usually are in globbing.
1633
1634 x# (Requires EXTENDED_GLOB to be set.) Matches zero or more occur‐
1635 rences of the pattern x. This operator has high precedence;
1636 `12#' is equivalent to `1(2#)', rather than `[1m(12)#'. It is an
1637 error for an unquoted `#' to follow something which cannot be
1638 repeated; this includes an empty string, a pattern already fol‐
1639 lowed by `##', or parentheses when part of a KSH_GLOB pattern
1640 (for example, `!(foo)#' is invalid and must be replaced by
1641 `*(!(foo))').
1642
1643 x## (Requires EXTENDED_GLOB to be set.) Matches one or more occur‐
1644 rences of the pattern x. This operator has high precedence;
1645 `12##' is equivalent to `1(2##)', rather than `[1m(12)##'. No more
1646 than two active `#' characters may appear together. (Note the
1647 potential clash with glob qualifiers in the form `1(2##)' which
1648 should therefore be avoided.)
1649
1650 ksh-like Glob Operators
1651 If the KSH_GLOB option is set, the effects of parentheses can be modi‐
1652 fied by a preceding `@', `*', `+', `?' or `!'. This character need not
1653 be unquoted to have special effects, but the `(' must be.
1654
1655 @(...) Match the pattern in the parentheses. (Like `(...)'.)
1656
1657 *(...) Match any number of occurrences. (Like `(...)#'.)
1658
1659 +(...) Match at least one occurrence. (Like `(...)##'.)
1660
1661 ?(...) Match zero or one occurrence. (Like `(|...)'.)
1662
1663 !(...) Match anything but the expression in parentheses. (Like
1664 `(^(...))'.)
1665
1666 Precedence
1667 The precedence of the operators given above is (highest) `^', `/', `~',
1668 `|' (lowest); the remaining operators are simply treated from left to
1669 right as part of a string, with `#' and `##' applying to the shortest
1670 possible preceding unit (i.e. a character, `?', `[...]', `<...>', or a
1671 parenthesised expression). As mentioned above, a `/' used as a direc‐
1672 tory separator may not appear inside parentheses, while a `|' must do
1673 so; in patterns used in other contexts than filename generation (for
1674 example, in case statements and tests within `[[...]]'), a `/' is not
1675 special; and `/' is also not special after a `~' appearing outside
1676 parentheses in a filename pattern.
1677
1678 Globbing Flags
1679 There are various flags which affect any text to their right up to the
1680 end of the enclosing group or to the end of the pattern; they require
1681 the EXTENDED_GLOB option. All take the form (#X) where X may have one
1682 of the following forms:
1683
1684 i Case insensitive: upper or lower case characters in the pattern
1685 match upper or lower case characters.
1686
1687 l Lower case characters in the pattern match upper or lower case
1688 characters; upper case characters in the pattern still only
1689 match upper case characters.
1690
1691 I Case sensitive: locally negates the effect of i or l from that
1692 point on.
1693
1694 b Activate backreferences for parenthesised groups in the pattern;
1695 this does not work in filename generation. When a pattern with
1696 a set of active parentheses is matched, the strings matched by
1697 the groups are stored in the array $match, the indices of the
1698 beginning of the matched parentheses in the array $mbegin, and
1699 the indices of the end in the array $mend, with the first ele‐
1700 ment of each array corresponding to the first parenthesised
1701 group, and so on. These arrays are not otherwise special to the
1702 shell. The indices use the same convention as does parameter
1703 substitution, so that elements of $mend and $mbegin may be used
1704 in subscripts; the KSH_ARRAYS option is respected. Sets of
1705 globbing flags are not considered parenthesised groups; only the
1706 first nine active parentheses can be referenced.
1707
1708 For example,
1709
1710 foo="a string with a message"
1711 if [[ $foo = (a|an)' '(#b)(*)' '* ]]; then
1712 print ${foo[$mbegin[1],$mend[1]]}
1713 fi
1714
1715 prints `string with a'. Note that the first parenthesis is
1716 before the (#b) and does not create a backreference.
1717
1718 Backreferences work with all forms of pattern matching other
1719 than filename generation, but note that when performing matches
1720 on an entire array, such as ${array#pattern}, or a global sub‐
1721 stitution, such as ${param//pat/repl}, only the data for the
1722 last match remains available. In the case of global replace‐
1723 ments this may still be useful. See the example for the m flag
1724 below.
1725
1726 The numbering of backreferences strictly follows the order of
1727 the opening parentheses from left to right in the pattern
1728 string, although sets of parentheses may be nested. There are
1729 special rules for parentheses followed by `#' or `##'. Only the
1730 last match of the parenthesis is remembered: for example, in `[[
1731 abab = (#b)([ab])# ]]', only the final `b' is stored in
1732 match[1]. Thus extra parentheses may be necessary to match the
1733 complete segment: for example, use `X((ab|cd)#)Y' to match a
1734 whole string of either `ab' or `cd' between `X' and `Y', using
1735 the value of $match[1] rather than $match[2].
1736
1737 If the match fails none of the parameters is altered, so in some
1738 cases it may be necessary to initialise them beforehand. If
1739 some of the backreferences fail to match -- which happens if
1740 they are in an alternate branch which fails to match, or if they
1741 are followed by # and matched zero times -- then the matched
1742 string is set to the empty string, and the start and end indices
1743 are set to -1.
1744
1745 Pattern matching with backreferences is slightly slower than
1746 without.
1747
1748 B Deactivate backreferences, negating the effect of the b flag
1749 from that point on.
1750
1751 cN,M The flag (#cN,M) can be used anywhere that the # or ## operators
1752 can be used except in the expressions `(*/)#' and `(*/)##' in
1753 filename generation, where `/' has special meaning; it cannot be
1754 combined with other globbing flags and a bad pattern error
1755 occurs if it is misplaced. It is equivalent to the form {N,M}
1756 in regular expressions. The previous character or group is
1757 required to match between N and M times, inclusive. The form
1758 (#cN) requires exactly N matches; (#c,M) is equivalent to speci‐
1759 fying N as 0; (#cN,) specifies that there is no maximum limit on
1760 the number of matches.
1761
1762 m Set references to the match data for the entire string matched;
1763 this is similar to backreferencing and does not work in filename
1764 generation. The flag must be in effect at the end of the pat‐
1765 tern, i.e. not local to a group. The parameters $MATCH, $MBEGIN
1766 and $MEND will be set to the string matched and to the indices
1767 of the beginning and end of the string, respectively. This is
1768 most useful in parameter substitutions, as otherwise the string
1769 matched is obvious.
1770
1771 For example,
1772
1773 arr=(veldt jynx grimps waqf zho buck)
1774 print ${arr//(#m)[aeiou]/${(U)MATCH}}
1775
1776 forces all the matches (i.e. all vowels) into uppercase, print‐
1777 ing `vEldt jynx grImps wAqf zhO bUck'.
1778
1779 Unlike backreferences, there is no speed penalty for using match
1780 references, other than the extra substitutions required for the
1781 replacement strings in cases such as the example shown.
1782
1783 M Deactivate the m flag, hence no references to match data will be
1784 created.
1785
1786 anum Approximate matching: num errors are allowed in the string
1787 matched by the pattern. The rules for this are described in the
1788 next subsection.
1789
1790 s, e Unlike the other flags, these have only a local effect, and each
1791 must appear on its own: `(#s)' and `(#e)' are the only valid
1792 forms. The `(#s)' flag succeeds only at the start of the test
1793 string, and the `(#e)' flag succeeds only at the end of the test
1794 string; they correspond to `^' and `$' in standard regular
1795 expressions. They are useful for matching path segments in pat‐
1796 terns other than those in filename generation (where path seg‐
1797 ments are in any case treated separately). For example,
1798 `*((#s)|/)test((#e)|/)*' matches a path segment `test' in any of
1799 the following strings: test, test/at/start, at/end/test,
1800 in/test/middle.
1801
1802 Another use is in parameter substitution; for example
1803 `${array/(#s)A*Z(#e)}' will remove only elements of an array
1804 which match the complete pattern `A*Z'. There are other ways of
1805 performing many operations of this type, however the combination
1806 of the substitution operations `/' and `//' with the `(#s)' and
1807 `(#e)' flags provides a single simple and memorable method.
1808
1809 Note that assertions of the form `(^(#s))' also work, i.e. match
1810 anywhere except at the start of the string, although this actu‐
1811 ally means `anything except a zero-length portion at the start
1812 of the string'; you need to use `(""~(#s))' to match a
1813 zero-length portion of the string not at the start.
1814
1815 q A `q' and everything up to the closing parenthesis of the glob‐
1816 bing flags are ignored by the pattern matching code. This is
1817 intended to support the use of glob qualifiers, see below. The
1818 result is that the pattern `(#b)(*).c(#q.)' can be used both for
1819 globbing and for matching against a string. In the former case,
1820 the `(#q.)' will be treated as a glob qualifier and the `(#b)'
1821 will not be useful, while in the latter case the `(#b)' is use‐
1822 ful for backreferences and the `(#q.)' will be ignored. Note
1823 that colon modifiers in the glob qualifiers are also not applied
1824 in ordinary pattern matching.
1825
1826 u Respect the current locale in determining the presence of multi‐
1827 byte characters in a pattern, provided the shell was compiled
1828 with MULTIBYTE_SUPPORT. This overrides the MULTIBYTE option;
1829 the default behaviour is taken from the option. Compare U.
1830 (Mnemonic: typically multibyte characters are from Unicode in
1831 the UTF-8 encoding, although any extension of ASCII supported by
1832 the system library may be used.)
1833
1834 U All characters are considered to be a single byte long. The
1835 opposite of u. This overrides the MULTIBYTE option.
1836
1837 For example, the test string fooxx can be matched by the pattern
1838 (#i)FOOXX, but not by (#l)FOOXX, (#i)FOO(#I)XX or ((#i)FOOX)X. The
1839 string (#ia2)readme specifies case-insensitive matching of readme with
1840 up to two errors.
1841
1842 When using the ksh syntax for grouping both KSH_GLOB and EXTENDED_GLOB
1843 must be set and the left parenthesis should be preceded by @. Note
1844 also that the flags do not affect letters inside [...] groups, in other
1845 words (#i)[a-z] still matches only lowercase letters. Finally, note
1846 that when examining whole paths case-insensitively every directory must
1847 be searched for all files which match, so that a pattern of the form
1848 (#i)/foo/bar/... is potentially slow.
1849
1850 Approximate Matching
1851 When matching approximately, the shell keeps a count of the errors
1852 found, which cannot exceed the number specified in the (#anum) flags.
1853 Four types of error are recognised:
1854
1855 1. Different characters, as in fooxbar and fooybar.
1856
1857 2. Transposition of characters, as in banana and abnana.
1858
1859 3. A character missing in the target string, as with the pattern
1860 road and target string rod.
1861
1862 4. An extra character appearing in the target string, as with stove
1863 and strove.
1864
1865 Thus, the pattern (#a3)abcd matches dcba, with the errors occurring by
1866 using the first rule twice and the second once, grouping the string as
1867 [d][cb][a] and [a][bc][d].
1868
1869 Non-literal parts of the pattern must match exactly, including charac‐
1870 ters in character ranges: hence (#a1)??? matches strings of length
1871 four, by applying rule 4 to an empty part of the pattern, but not
1872 strings of length two, since all the ? must match. Other characters
1873 which must match exactly are initial dots in filenames (unless the
1874 GLOB_DOTS option is set), and all slashes in filenames, so that a/bc is
1875 two errors from ab/c (the slash cannot be transposed with another char‐
1876 acter). Similarly, errors are counted separately for non-contiguous
1877 strings in the pattern, so that (ab|cd)ef is two errors from aebf.
1878
1879 When using exclusion via the ~ operator, approximate matching is
1880 treated entirely separately for the excluded part and must be activated
1881 separately. Thus, (#a1)README~READ_ME matches READ.ME but not READ_ME,
1882 as the trailing READ_ME is matched without approximation. However,
1883 (#a1)README~(#a1)READ_ME does not match any pattern of the form READ?ME
1884 as all such forms are now excluded.
1885
1886 Apart from exclusions, there is only one overall error count; however,
1887 the maximum errors allowed may be altered locally, and this can be
1888 delimited by grouping. For example, (#a1)cat((#a0)dog)fox allows one
1889 error in total, which may not occur in the dog section, and the pattern
1890 (#a1)cat(#a0)dog(#a1)fox is equivalent. Note that the point at which
1891 an error is first found is the crucial one for establishing whether to
1892 use approximation; for example, (#a1)abc(#a0)xyz will not match
1893 abcdxyz, because the error occurs at the `x', where approximation is
1894 turned off.
1895
1896 Entire path segments may be matched approximately, so that
1897 `(#a1)/foo/d/is/available/at/the/bar' allows one error in any path seg‐
1898 ment. This is much less efficient than without the (#a1), however,
1899 since every directory in the path must be scanned for a possible
1900 approximate match. It is best to place the (#a1) after any path seg‐
1901 ments which are known to be correct.
1902
1903 Recursive Globbing
1904 A pathname component of the form `(foo/)#' matches a path consisting of
1905 zero or more directories matching the pattern foo.
1906
1907 As a shorthand, `**/' is equivalent to `(*/)#'; note that this there‐
1908 fore matches files in the current directory as well as subdirectories.
1909 Thus:
1910
1911 ls (*/)#bar
1912
1913 or
1914
1915 ls **/bar
1916
1917 does a recursive directory search for files named `bar' (potentially
1918 including the file `bar' in the current directory). This form does not
1919 follow symbolic links; the alternative form `***/' does, but is other‐
1920 wise identical. Neither of these can be combined with other forms of
1921 globbing within the same path segment; in that case, the `*' operators
1922 revert to their usual effect.
1923
1924 Glob Qualifiers
1925 Patterns used for filename generation may end in a list of qualifiers
1926 enclosed in parentheses. The qualifiers specify which filenames that
1927 otherwise match the given pattern will be inserted in the argument
1928 list.
1929
1930 If the option BARE_GLOB_QUAL is set, then a trailing set of parentheses
1931 containing no `|' or `(' characters (or `~' if it is special) is taken
1932 as a set of glob qualifiers. A glob subexpression that would normally
1933 be taken as glob qualifiers, for example `(^x)', can be forced to be
1934 treated as part of the glob pattern by doubling the parentheses, in
1935 this case producing `((^x))'.
1936
1937 If the option EXTENDED_GLOB is set, a different syntax for glob quali‐
1938 fiers is available, namely `(#qx)' where x is any of the same glob
1939 qualifiers used in the other format. The qualifiers must still appear
1940 at the end of the pattern. However, with this syntax multiple glob
1941 qualifiers may be chained together. They are treated as a logical AND
1942 of the individual sets of flags. Also, as the syntax is unambiguous,
1943 the expression will be treated as glob qualifiers just as long any
1944 parentheses contained within it are balanced; appearance of `|', `(' or
1945 `~' does not negate the effect. Note that qualifiers will be recog‐
1946 nised in this form even if a bare glob qualifier exists at the end of
1947 the pattern, for example `*(#q*)(.)' will recognise executable regular
1948 files if both options are set; however, mixed syntax should probably be
1949 avoided for the sake of clarity.
1950
1951 A qualifier may be any one of the following:
1952
1953 / directories
1954
1955 F `full' (i.e. non-empty) directories. Note that the opposite
1956 sense (^F) expands to empty directories and all non-directories.
1957 Use (/^F) for empty directories.
1958
1959 . plain files
1960
1961 @ symbolic links
1962
1963 = sockets
1964
1965 p named pipes (FIFOs)
1966
1967 * executable plain files (0100)
1968
1969 % device files (character or block special)
1970
1971 %b block special files
1972
1973 %c character special files
1974
1975 r owner-readable files (0400)
1976
1977 w owner-writable files (0200)
1978
1979 x owner-executable files (0100)
1980
1981 A group-readable files (0040)
1982
1983 I group-writable files (0020)
1984
1985 E group-executable files (0010)
1986
1987 R world-readable files (0004)
1988
1989 W world-writable files (0002)
1990
1991 X world-executable files (0001)
1992
1993 s setuid files (04000)
1994
1995 S setgid files (02000)
1996
1997 t files with the sticky bit (01000)
1998
1999 fspec files with access rights matching spec. This spec may be a octal
2000 number optionally preceded by a `=', a `+', or a `-'. If none of
2001 these characters is given, the behavior is the same as for `='.
2002 The octal number describes the mode bits to be expected, if com‐
2003 bined with a `=', the value given must match the file-modes
2004 exactly, with a `+', at least the bits in the given number must
2005 be set in the file-modes, and with a `-', the bits in the number
2006 must not be set. Giving a `?' instead of a octal digit anywhere
2007 in the number ensures that the corresponding bits in the
2008 file-modes are not checked, this is only useful in combination
2009 with `='.
2010
2011 If the qualifier `f' is followed by any other character anything
2012 up to the next matching character (`[', `{', and `<' match `]',
2013 `}', and `>' respectively, any other character matches itself)
2014 is taken as a list of comma-separated sub-specs. Each sub-spec
2015 may be either an octal number as described above or a list of
2016 any of the characters `u', `g', `o', and `a', followed by a `=',
2017 a `+', or a `-', followed by a list of any of the characters
2018 `r', `w', `x', `s', and `t', or an octal digit. The first list
2019 of characters specify which access rights are to be checked. If
2020 a `u' is given, those for the owner of the file are used, if a
2021 `g' is given, those of the group are checked, a `o' means to
2022 test those of other users, and the `a' says to test all three
2023 groups. The `=', `+', and `-' again says how the modes are to be
2024 checked and have the same meaning as described for the first
2025 form above. The second list of characters finally says which
2026 access rights are to be expected: `r' for read access, `w' for
2027 write access, `x' for the right to execute the file (or to
2028 search a directory), `s' for the setuid and setgid bits, and `t'
2029 for the sticky bit.
2030
2031 Thus, `*(f70?)' gives the files for which the owner has read,
2032 write, and execute permission, and for which other group members
2033 have no rights, independent of the permissions for other users.
2034 The pattern `*(f-100)' gives all files for which the owner does
2035 not have execute permission, and `*(f:gu+w,o-rx:)' gives the
2036 files for which the owner and the other members of the group
2037 have at least write permission, and for which other users don't
2038 have read or execute permission.
2039
2040 estring
2041 +cmd The string will be executed as shell code. The filename will be
2042 included in the list if and only if the code returns a zero sta‐
2043 tus (usually the status of the last command).
2044
2045 In the first form, the first character after the `e' will be
2046 used as a separator and anything up to the next matching separa‐
2047 tor will be taken as the string; `[', `{', and `<' match `]',
2048 `}', and `>', respectively, while any other character matches
2049 itself. Note that expansions must be quoted in the string to
2050 prevent them from being expanded before globbing is done.
2051 string is then executed as shell code. The string globqual is
2052 appended to the array zsh_eval_context the duration of execu‐
2053 tion.
2054
2055 During the execution of string the filename currently being
2056 tested is available in the parameter REPLY; the parameter may be
2057 altered to a string to be inserted into the list instead of the
2058 original filename. In addition, the parameter reply may be set
2059 to an array or a string, which overrides the value of REPLY. If
2060 set to an array, the latter is inserted into the command line
2061 word by word.
2062
2063 For example, suppose a directory contains a single file
2064 `lonely'. Then the expression `*(e:'reply=(${REPLY}{1,2})':)'
2065 will cause the words `lonely1' and `lonely2' to be inserted into
2066 the command line. Note the quoting of string.
2067
2068 The form +cmd has the same effect, but no delimiters appear
2069 around cmd. Instead, cmd is taken as the longest sequence of
2070 characters following the + that are alphanumeric or underscore.
2071 Typically cmd will be the name of a shell function that contains
2072 the appropriate test. For example,
2073
2074 nt() { [[ $REPLY -nt $NTREF ]] }
2075 NTREF=reffile
2076 ls -l *(+nt)
2077
2078 lists all files in the directory that have been modified more
2079 recently than reffile.
2080
2081 ddev files on the device dev
2082
2083 l[-|+]ct
2084 files having a link count less than ct (-), greater than ct (+),
2085 or equal to ct
2086
2087 U files owned by the effective user ID
2088
2089 G files owned by the effective group ID
2090
2091 uid files owned by user ID id if that is a number. Otherwise, id
2092 specifies a user name: the character after the `u' will be taken
2093 as a separator and the string between it and the next matching
2094 separator will be taken as a user name. The starting separators
2095 `[', `{', and `<' match the final separators `]', `}', and `>',
2096 respectively; any other character matches itself. The selected
2097 files are those owned by this user. For example, `u:foo:' or
2098 `u[foo]' selects files owned by user `foo'.
2099
2100 gid like uid but with group IDs or names
2101
2102 a[Mwhms][-|+]n
2103 files accessed exactly n days ago. Files accessed within the
2104 last n days are selected using a negative value for n (-n).
2105 Files accessed more than n days ago are selected by a positive n
2106 value (+n). Optional unit specifiers `M', `w', `h', `m' or `s'
2107 (e.g. `ah5') cause the check to be performed with months (of 30
2108 days), weeks, hours, minutes or seconds instead of days, respec‐
2109 tively. An explicit `d' for days is also allowed.
2110
2111 Any fractional part of the difference between the access time
2112 and the current part in the appropriate units is ignored in the
2113 comparison. For instance, `echo *(ah-5)' would echo files
2114 accessed within the last five hours, while `echo *(ah+5)' would
2115 echo files accessed at least six hours ago, as times strictly
2116 between five and six hours are treated as five hours.
2117
2118 m[Mwhms][-|+]n
2119 like the file access qualifier, except that it uses the file
2120 modification time.
2121
2122 c[Mwhms][-|+]n
2123 like the file access qualifier, except that it uses the file
2124 inode change time.
2125
2126 L[+|-]n
2127 files less than n bytes (-), more than n bytes (+), or exactly n
2128 bytes in length.
2129
2130 If this flag is directly followed by a `k' (`K'), `m' (`M'), or
2131 `p' (`P') (e.g. `Lk-50') the check is performed with kilobytes,
2132 megabytes, or blocks (of 512 bytes) instead. In this case a
2133 file is regarded as "exactly" the size if the file size rounded
2134 up to the next unit is equal to the test size. Hence `*(Lm1)'
2135 matches files from 1 byte up to 1 Megabyte inclusive. Note also
2136 that the set of files "less than" the test size only includes
2137 files that would not match the equality test; hence `*(Lm-1)'
2138 only matches files of zero size.
2139
2140 ^ negates all qualifiers following it
2141
2142 - toggles between making the qualifiers work on symbolic links
2143 (the default) and the files they point to
2144
2145 M sets the MARK_DIRS option for the current pattern
2146
2147 T appends a trailing qualifier mark to the filenames, analogous to
2148 the LIST_TYPES option, for the current pattern (overrides M)
2149
2150 N sets the NULL_GLOB option for the current pattern
2151
2152 D sets the GLOB_DOTS option for the current pattern
2153
2154 n sets the NUMERIC_GLOB_SORT option for the current pattern
2155
2156 oc specifies how the names of the files should be sorted. If c is n
2157 they are sorted by name (the default); if it is L they are
2158 sorted depending on the size (length) of the files; if l they
2159 are sorted by the number of links; if a, m, or c they are sorted
2160 by the time of the last access, modification, or inode change
2161 respectively; if d, files in subdirectories appear before those
2162 in the current directory at each level of the search -- this is
2163 best combined with other criteria, for example `odon' to sort on
2164 names for files within the same directory; if N, no sorting is
2165 performed. Note that a, m, and c compare the age against the
2166 current time, hence the first name in the list is the youngest
2167 file. Also note that the modifiers ^ and - are used, so
2168 `*(^-oL)' gives a list of all files sorted by file size in
2169 descending order, following any symbolic links. Unless oN is
2170 used, multiple order specifiers may occur to resolve ties.
2171
2172 oe and o+ are special cases; they are each followed by shell
2173 code, delimited as for the e glob qualifier and the + glob qual‐
2174 ifier respectively (see above). The code is executed for each
2175 matched file with the parameter REPLY set to the name of the
2176 file on entry and globsort appended to zsh_eval_context. The
2177 code should modify the parameter REPLY in some fashion. On
2178 return, the value of the parameter is used instead of the file
2179 name as the string on which to sort. Unlike other sort opera‐
2180 tors, oe and o+ may be repeated, but note that the maximum num‐
2181 ber of sort operators of any kind that may appear in any glob
2182 expression is 12.
2183
2184 Oc like `o', but sorts in descending order; i.e. `*(^oc)' is the
2185 same as `*(Oc)' and `*(^Oc)' is the same as `*(oc)'; `Od' puts
2186 files in the current directory before those in subdirectories at
2187 each level of the search.
2188
2189 [beg[,end]]
2190 specifies which of the matched filenames should be included in
2191 the returned list. The syntax is the same as for array sub‐
2192 scripts. beg and the optional end may be mathematical expres‐
2193 sions. As in parameter subscripting they may be negative to make
2194 them count from the last match backward. E.g.: `*(-OL[1,3])'
2195 gives a list of the names of the three largest files.
2196
2197 Pstring
2198 The string will be prepended to each glob match as a separate
2199 word. string is delimited in the same way as arguments to the e
2200 glob qualifier described above. The qualifier can be repeated;
2201 the words are prepended separately so that the resulting command
2202 line contains the words in the same order they were given in the
2203 list of glob qualifiers.
2204
2205 A typical use for this is to prepend an option before all occur‐
2206 rences of a file name; for example, the pattern `*(P:-f:)' pro‐
2207 duces the command line arguments `-f file1 -f file2 ...'
2208
2209 More than one of these lists can be combined, separated by commas. The
2210 whole list matches if at least one of the sublists matches (they are
2211 `or'ed, the qualifiers in the sublists are `and'ed). Some qualifiers,
2212 however, affect all matches generated, independent of the sublist in
2213 which they are given. These are the qualifiers `M', `T', `N', `D',
2214 `n', `o', `O' and the subscripts given in brackets (`[...]').
2215
2216 If a `:' appears in a qualifier list, the remainder of the expression
2217 in parenthesis is interpreted as a modifier (see the section `Modi‐
2218 fiers' in the section `History Expansion'). Each modifier must be
2219 introduced by a separate `:'. Note also that the result after modifi‐
2220 cation does not have to be an existing file. The name of any existing
2221 file can be followed by a modifier of the form `(:..)' even if no
2222 actual filename generation is performed, although note that the pres‐
2223 ence of the parentheses causes the entire expression to be subjected to
2224 any global pattern matching options such as NULL_GLOB. Thus:
2225
2226 ls *(-/)
2227
2228 lists all directories and symbolic links that point to directories, and
2229
2230 ls *(%W)
2231
2232 lists all world-writable device files in the current directory, and
2233
2234 ls *(W,X)
2235
2236 lists all files in the current directory that are world-writable or
2237 world-executable, and
2238
2239 echo /tmp/foo*(u0^@:t)
2240
2241 outputs the basename of all root-owned files beginning with the string
2242 `foo' in /tmp, ignoring symlinks, and
2243
2244 ls *.*~(lex|parse).[ch](^D^l1)
2245
2246 lists all files having a link count of one whose names contain a dot
2247 (but not those starting with a dot, since GLOB_DOTS is explicitly
2248 switched off) except for lex.c, lex.h, parse.c and parse.h.
2249
2250 print b*.pro(#q:s/pro/shmo/)(#q.:s/builtin/shmiltin/)
2251
2252 demonstrates how colon modifiers and other qualifiers may be chained
2253 together. The ordinary qualifier `.' is applied first, then the colon
2254 modifiers in order from left to right. So if EXTENDED_GLOB is set and
2255 the base pattern matches the regular file builtin.pro, the shell will
2256 print `shmiltin.shmo'.
2257
2258
2259
2260zsh 5.0.2 December 21, 2012 ZSHEXPN(1)