zsh: Parameter Expansion

Next: Command Substitution, Previous: Process Substitution, Up: Expansion [Contents][Index]

### 14.3 Parameter Expansion

The character ‘`$`’ is used to introduce parameter expansions. See Parameters for a description of parameters, including arrays, associative arrays, and subscript notation to access individual array elements.

Note in particular the fact that words of unquoted parameters are not automatically split on whitespace unless the option `SH_WORD_SPLIT` is set; see references to this option below for more details. This is an important difference from other shells.

In the expansions discussed below that require a pattern, the form of the pattern is the same as that used for filename generation; see Filename Generation. Note that these patterns, along with the replacement text of any substitutions, are themselves subject to parameter expansion, command substitution, and arithmetic expansion. In addition to the following operations, the colon modifiers described in Modifiers in History Expansion can be applied: for example, `${i:s/foo/bar/}` performs string substitution on the expansion of parameter `$i`.

`${`name`}`

  The value, if any, of the parameter name is substituted. The braces are required if the expansion is to be followed by a letter, digit, or underscore that is not to be interpreted as part of name. In addition, more complicated forms of substitution usually require the braces to be present; exceptions, which only apply if the option `KSH_ARRAYS` is not set, are a single subscript or any colon modifiers appearing after the name, or any of the characters ‘`^`’, ‘`=`’, ‘`~`’, ‘`#`’ or ‘`+`’ appearing before the name, all of which work with or without braces.

  If name is an array parameter, and the `KSH_ARRAYS` option is not set, then the value of each element of name is substituted, one element per word. Otherwise, the expansion results in one word only; with `KSH_ARRAYS`, this is the first element of an array. No field splitting is done on the result unless the `SH_WORD_SPLIT` option is set. See also the flags `=` and `s:` string`:`.

`${+`name`}`

  If name is the name of a set parameter ‘`1`’ is substituted, otherwise ‘`0`’ is substituted.

`${`name`-`word`}`
`${`name`:-`word`}`

  If name is set, or in the second form is non-null, then substitute its value; otherwise substitute word. In the second form name may be omitted, in which case word is always substituted.

`${`name`+`word`}`
`${`name`:+`word`}`

  If name is set, or in the second form is non-null, then substitute word; otherwise substitute nothing.

`${`name`=`word`}`
`${`name`:=`word`}`
`${`name`::=`word`}`

  In the first form, if name is unset then set it to word; in the second form, if name is unset or null then set it to word; and in the third form, unconditionally set name to word. In all forms, the value of the parameter is then substituted.

`${`name`?`word`}`
`${`name`:?`word`}`

  In the first form, if name is set, or in the second form if name is both set and non-null, then substitute its value; otherwise, print word and exit from the shell. Interactive shells instead return to the prompt. If word is omitted, then a standard message is printed.

In any of the above expressions that test a variable and substitute an alternate word, note that you can use standard shell quoting in the word value to selectively override the splitting done by the `SH_WORD_SPLIT` option and the `=` flag, but not splitting by the `s:` string`:` flag.

In the following expressions, when name is an array and the substitution is not quoted, or if the ‘`(@)`’ flag or the name`[@]` syntax is used, matching and replacement is performed on each array element separately.

`${`name`#`pattern`}`
`${`name`##`pattern`}`

  If the pattern matches the beginning of the value of name, then substitute the value of name with the matched portion deleted; otherwise, just substitute the value of name. In the first form, the smallest matching pattern is preferred; in the second form, the largest matching pattern is preferred.

`${`name`%`pattern`}`
`${`name`%%`pattern`}`

  If the pattern matches the end of the value of name, then substitute the value of name with the matched portion deleted; otherwise, just substitute the value of name. In the first form, the smallest matching pattern is preferred; in the second form, the largest matching pattern is preferred.

`${`name`:#`pattern`}`

  If the pattern matches the value of name, then substitute the empty string; otherwise, just substitute the value of name. If name is an array the matching array elements are removed (use the ‘`(M)`’ flag to remove the non-matched elements).

`${`name`:|`arrayname`}`

  If arrayname is the name (N.B., not contents) of an array variable, then any elements contained in arrayname are removed from the substitution of name. If the substitution is scalar, either because name is a scalar variable or the expression is quoted, the elements of arrayname are instead tested against the entire expression.

`${`name`:*`arrayname`}`

  Similar to the preceding substitution, but in the opposite sense, so that entries present in both the original substitution and as elements of arrayname are retained and others removed.

`${`name`:^`arrayname`}`
`${`name`:^^`arrayname`}`

  Zips two arrays, such that the output array is twice as long as the shortest (longest for ‘`:^^`’) of `name` and `arrayname`, with the elements alternatingly being picked from them. For ‘`:^`’, if one of the input arrays is longer, the output will stop when the end of the shorter array is reached. Thus,

      a=(1 2 3 4); b=(a b); print ${a:^b}

  will output ‘`1 a 2 b`’. For ‘`:^^`’, then the input is repeated until all of the longer array has been used up and the above will output ‘`1 a 2 b 3 a 4 b`’.

  Either or both inputs may be a scalar, they will be treated as an array of length 1 with the scalar as the only element. If either array is empty, the other array is output with no extra elements inserted.

  Currently the following code will output ‘`a b`’ and ‘`1`’ as two separate elements, which can be unexpected. The second print provides a workaround which should continue to work if this is changed.

      a=(a b); b=(1 2); print -l "${a:^b}"; print -l "${${a:^b}}"

`${`name`:`offset`}`
`${`name`:`offset`:`length`}`

  This syntax gives effects similar to parameter subscripting in the form `$`name`[`start`,`end`]`, but is compatible with other shells; note that both offset and length are interpreted differently from the components of a subscript.

  If offset is non-negative, then if the variable name is a scalar substitute the contents starting offset characters from the first character of the string, and if name is an array substitute elements starting offset elements from the first element. If length is given, substitute that many characters or elements, otherwise the entire rest of the scalar or array.

  A positive offset is always treated as the offset of a character or element in name from the first character or element of the array (this is different from native zsh subscript notation). Hence 0 refers to the first character or element regardless of the setting of the option `KSH_ARRAYS`.

  A negative offset counts backwards from the end of the scalar or array, so that -1 corresponds to the last character or element, and so on.

  When positive, length counts from the offset position toward the end of the scalar or array. When negative, length counts back from the end. If this results in a position smaller than offset, a diagnostic is printed and nothing is substituted.

  The option `MULTIBYTE` is obeyed, i.e. the offset and length count multibyte characters where appropriate.

  offset and length undergo the same set of shell substitutions as for scalar assignment; in addition, they are then subject to arithmetic evaluation. Hence, for example

      print ${foo:3}
      print ${foo: 1 + 2}
      print ${foo:$(( 1 + 2))}
      print ${foo:$(echo 1 + 2)}

  all have the same effect, extracting the string starting at the fourth character of `$foo` if the substitution would otherwise return a scalar, or the array starting at the fourth element if `$foo` would return an array. Note that with the option `KSH_ARRAYS` `$foo` always returns a scalar (regardless of the use of the offset syntax) and a form such as `$foo[*]:3` is required to extract elements of an array named `foo`.

  If offset is negative, the `-` may not appear immediately after the `:` as this indicates the `${`name`:-`word`}` form of substitution. Instead, a space may be inserted before the `-`. Furthermore, neither offset nor length may begin with an alphabetic character or `&` as these are used to indicate history-style modifiers. To substitute a value from a variable, the recommended approach is to precede it with a `$` as this signifies the intention (parameter substitution can easily be rendered unreadable); however, as arithmetic substitution is performed, the expression `${var: offs}` does work, retrieving the offset from `$offs`.

  For further compatibility with other shells there is a special case for array offset 0. This usually accesses to the first element of the array. However, if the substitution refers the positional parameter array, e.g. `$@` or `$*`, then offset 0 instead refers to `$0`, offset 1 refers to `$1`, and so on. In other words, the positional parameter array is effectively extended by prepending `$0`. Hence `${*:0:1}` substitutes `$0` and `${*:1:1}` substitutes `$1`.

`${`name`/`pattern`/`repl`}`
`${`name`//`pattern`/`repl`}`

  Replace the longest possible match of pattern in the expansion of parameter name by string repl. The first form replaces just the first occurrence, the second form all occurrences. Both pattern and repl are subject to double-quoted substitution, so that expressions like `${name/$opat/$npat}` will work, but note the usual rule that pattern characters in `$opat` are not treated specially unless either the option `GLOB_SUBST` is set, or `$opat` is instead substituted as `${~opat}`.

  The pattern may begin with a ‘`#`’, in which case the pattern must match at the start of the string, or ‘`%`’, in which case it must match at the end of the string, or ‘`#%`’ in which case the pattern must match the entire string. The repl may be an empty string, in which case the final ‘`/`’ may also be omitted. To quote the final ‘`/`’ in other cases it should be preceded by a single backslash; this is not necessary if the ‘`/`’ occurs inside a substituted parameter. Note also that the ‘`#`’, ‘`%`’ and ‘`#%` are not active if they occur inside a substituted parameter, even at the start.

  The first ‘`/`’ may be preceded by a ‘`:`’, in which case the match will only succeed if it matches the entire word. Note also the effect of the `I` and `S` parameter expansion flags below; however, the flags `M`, `R`, `B`, `E` and `N` are not useful.

  For example,

      foo="twinkle twinkle little star" sub="t*e" rep="spy"
      print ${foo//${~sub}/$rep}
      print ${(S)foo//${~sub}/$rep}

  Here, the ‘`~`’ ensures that the text of `$sub` is treated as a pattern rather than a plain string. In the first case, the longest match for `t*e` is substituted and the result is ‘`spy star`’, while in the second case, the shortest matches are taken and the result is ‘`spy spy lispy star`’.

`${#`spec`}`

  If spec is one of the above substitutions, substitute the length in characters of the result instead of the result itself. If spec is an array expression, substitute the number of elements of the result. This has the side-effect that joining is skipped even in quoted forms, which may affect other sub-expressions in spec. Note that ‘`^`’, ‘`=`’, and ‘`~`’, below, must appear to the left of ‘`#`’ when these forms are combined.

  If the option `POSIX_IDENTIFIERS` is not set, and spec is a simple name, then the braces are optional; this is true even for special parameters so e.g. `$#-` and `$#*` take the length of the string `$-` and the array `$*` respectively. If `POSIX_IDENTIFIERS` is set, then braces are required for the `#` to be treated in this fashion.

`${^`spec`}`

  Turn on the `RC_EXPAND_PARAM` option for the evaluation of spec; if the ‘`^`’ is doubled, turn it off. When this option is set, array expansions of the form foo`${`xx`}`bar, where the parameter xx is set to `(`a b c`)`, are substituted with ‘fooabar foobbar foocbar’ instead of the default ‘fooa b cbar’. Note that an empty array will therefore cause all arguments to be removed.

  Internally, each such expansion is converted into the equivalent list for brace expansion. E.g., `${^var}` becomes `{$var[1],$var[2],`...`}`, and is processed as described in Brace Expansion below. If word splitting is also in effect the `$var[`N`]` may themselves be split into different list elements.

`${=`spec`}`

  Perform word splitting using the rules for `SH_WORD_SPLIT` during the evaluation of spec, but regardless of whether the parameter appears in double quotes; if the ‘`=`’ is doubled, turn it off. This forces parameter expansions to be split into separate words before substitution, using `IFS` as a delimiter. This is done by default in most other shells.

  Note that splitting is applied to word in the assignment forms of spec before the assignment to name is performed. This affects the result of array assignments with the `A` flag.

`${~`spec`}`

  Turn on the `GLOB_SUBST` option for the evaluation of spec; if the ‘`~`’ is doubled, turn it off. When this option is set, the string resulting from the expansion will be interpreted as a pattern anywhere that is possible, such as in filename expansion and filename generation and pattern-matching contexts like the right hand side of the ‘`=`’ and ‘`!=`’ operators in conditions.

  In nested substitutions, note that the effect of the `~` applies to the result of the current level of substitution. A surrounding pattern operation on the result may cancel it. Hence, for example, if the parameter `foo` is set to `*`, `${~foo//\*/*.c}` is substituted by the pattern `*.c`, which may be expanded by filename generation, but `${${~foo}//\*/*.c}` substitutes to the string `*.c`, which will not be further expanded.

If a `${`...`}` type parameter expression or a `$(`...`)` type command substitution is used in place of name above, it is expanded first and the result is used as if it were the value of name. Thus it is possible to perform nested operations: `${${foo#head}%tail}` substitutes the value of `$foo` with both ‘`head`’ and ‘`tail`’ deleted. The form with `$(`...`)` is often useful in combination with the flags described next; see the examples below. Each name or nested `${`...`}` in a parameter expansion may also be followed by a subscript expression as described in Array Parameters.

Note that double quotes may appear around nested expressions, in which case only the part inside is treated as quoted; for example, `${(f)"$(foo)"}` quotes the result of `$(foo)`, but the flag ‘`(f)`’ (see below) is applied using the rules for unquoted expansions. Note further that quotes are themselves nested in this context; for example, in `"${(@f)"$(foo)"}"`, there are two sets of quotes, one surrounding the whole expression, the other (redundant) surrounding the `$(foo)` as before.

#### 14.3.1 Parameter Expansion Flags

If the opening brace is directly followed by an opening parenthesis, the string up to the matching closing parenthesis will be taken as a list of flags. In cases where repeating a flag is meaningful, the repetitions need not be consecutive; for example, ‘(`q%q%q`)’ means the same thing as the more readable ‘(`%%qqq`)’. The following flags are supported:

`#`

  Evaluate the resulting words as numeric expressions and output the characters corresponding to the resulting integer. Note that this form is entirely distinct from use of the `#` without parentheses.

  If the `MULTIBYTE` option is set and the number is greater than 127 (i.e. not an ASCII character) it is treated as a Unicode character.

`%`

  Expand all `%` escapes in the resulting words in the same way as in prompts (see Prompt Expansion). If this flag is given twice, full prompt expansion is done on the resulting words, depending on the setting of the `PROMPT_PERCENT`, `PROMPT_SUBST` and `PROMPT_BANG` options.

`@`

  In double quotes, array elements are put into separate words. E.g., ‘`"${(@)foo}"`’ is equivalent to ‘`"${foo[@]}"`’ and ‘`"${(@)foo[1,2]}"`’ is the same as ‘`"$foo[1]" "$foo[2]"`’. This is distinct from field splitting by the `f`, `s` or `z` flags, which still applies within each array element.

`A`

  Create an array parameter with ‘`${`...`=`...`}`’, ‘`${`...`:=`...`}`’ or ‘`${`...`::=`...`}`’. If this flag is repeated (as in ‘`AA`’), create an associative array parameter. Assignment is made before sorting or padding; if field splitting is active, the word part is split before assignment. The name part may be a subscripted range for ordinary arrays; the word part must be converted to an array, for example by using ‘`${(AA)=`name`=`...`}`’ to activate field splitting, when creating an associative array.

`a`

  Sort in array index order; when combined with ‘`O`’ sort in reverse array index order. Note that ‘`a`’ is therefore equivalent to the default but ‘`Oa`’ is useful for obtaining an array’s elements in reverse order.

`b`

  Quote with backslashes only characters that are special to pattern matching. This is useful when the contents of the variable are to be tested using `GLOB_SUBST`, including the `${~`...`}` switch.

  Quoting using one of the `q` family of flags does not work for this purpose since quotes are not stripped from non-pattern characters by `GLOB_SUBST`. In other words,

      pattern=${(q)str}
      [[ $str = ${~pattern} ]]

  works if `$str` is ‘`a*b`’ but not if it is ‘`a b`’, whereas

      pattern=${(b)str}
      [[ $str = ${~pattern} ]]

  is always true for any possible value of `$str`.

`c`

  With `${#`name`}`, count the total number of characters in an array, as if the elements were concatenated with spaces between them. This is not a true join of the array, so other expressions used with this flag may have an effect on the elements of the array before it is counted.

`C`

  Capitalize the resulting words. ‘Words’ in this case refers to sequences of alphanumeric characters separated by non-alphanumerics, not to words that result from field splitting.

`D`

  Assume the string or array elements contain directories and attempt to substitute the leading part of these by names. The remainder of the path (the whole of it if the leading part was not substituted) is then quoted so that the whole string can be used as a shell argument. This is the reverse of ‘`~`’ substitution: see Filename Expansion.

`e`

  Perform parameter expansion, command substitution and arithmetic expansion on the result. Such expansions can be nested but too deep recursion may have unpredictable effects.

`f`

  Split the result of the expansion at newlines. This is a shorthand for ‘`ps:\n:`’.

`F`

  Join the words of arrays together using newline as a separator. This is a shorthand for ‘`pj:\n:`’.

`g:` opts`:`

  Process escape sequences like the echo builtin when no options are given (`g::`). With the `o` option, octal escapes don’t take a leading zero. With the `c` option, sequences like ‘`^X`’ are also processed. With the `e` option, processes ‘`\M-t`’ and similar sequences like the print builtin. With both of the `o` and `e` options, behaves like the print builtin except that in none of these modes is ‘`\c`’ interpreted.

`i`

  Sort case-insensitively. May be combined with ‘`n`’ or ‘`O`’.

`k`

  If name refers to an associative array, substitute the keys (element names) rather than the values of the elements. Used with subscripts (including ordinary arrays), force indices or keys to be substituted even if the subscript form refers to values. However, this flag may not be combined with subscript ranges.

`L`

  Convert all letters in the result to lower case.

`n`

  Sort decimal integers numerically; if the first differing characters of two test strings are not digits, sorting is lexical. Integers with more initial zeroes are sorted before those with fewer or none. Hence the array ‘`foo1 foo02 foo2 foo3 foo20 foo23`’ is sorted into the order shown. May be combined with ‘`i`’ or ‘`O`’.

`o`

  Sort the resulting words in ascending order; if this appears on its own the sorting is lexical and case-sensitive (unless the locale renders it case-insensitive). Sorting in ascending order is the default for other forms of sorting, so this is ignored if combined with ‘`a`’, ‘`i`’ or ‘`n`’.

`O`

  Sort the resulting words in descending order; ‘`O`’ without ‘`a`’, ‘`i`’ or ‘`n`’ sorts in reverse lexical order. May be combined with ‘`a`’, ‘`i`’ or ‘`n`’ to reverse the order of sorting.

`P`

  This forces the value of the parameter name to be interpreted as a further parameter name, whose value will be used where appropriate. Note that flags set with one of the `typeset` family of commands (in particular case transformations) are not applied to the value of name used in this fashion.

  If used with a nested parameter or command substitution, the result of that will be taken as a parameter name in the same way. For example, if you have ‘`foo=bar`’ and ‘`bar=baz`’, the strings `${(P)foo}`, `${(P)${foo}}`, and `${(P)$(echo bar)}` will be expanded to ‘`baz`’.

  Likewise, if the reference is itself nested, the expression with the flag is treated as if it were directly replaced by the parameter name. It is an error if this nested substitution produces an array with more than one word. For example, if ‘`name=assoc`’ where the parameter `assoc` is an associative array, then ‘`${${(P)name}[elt]}`’ refers to the element of the associative subscripted ‘`elt`’.

`q`

  Quote characters that are special to the shell in the resulting words with backslashes; unprintable or invalid characters are quoted using the `$'\`NNN`'` form, with separate quotes for each octet.

  If this flag is given twice, the resulting words are quoted in single quotes and if it is given three times, the words are quoted in double quotes; in these forms no special handling of unprintable or invalid characters is attempted. If the flag is given four times, the words are quoted in single quotes preceded by a `$`. Note that in all three of these forms quoting is done unconditionally, even if this does not change the way the resulting string would be interpreted by the shell.

  If a `q-` is given (only a single `q` may appear), a minimal form of single quoting is used that only quotes the string if needed to protect special characters. Typically this form gives the most readable output.

`Q`

  Remove one level of quotes from the resulting words.

`t`

  Use a string describing the type of the parameter where the value of the parameter would usually appear. This string consists of keywords separated by hyphens (‘`-`’). The first keyword in the string describes the main type, it can be one of ‘`scalar`’, ‘`array`’, ‘`integer`’, ‘`float`’ or ‘`association`’. The other keywords describe the type in more detail:

  `local`

    for local parameters

  `left`

    for left justified parameters

  `right_blanks`

    for right justified parameters with leading blanks

  `right_zeros`

    for right justified parameters with leading zeros

  `lower`

    for parameters whose value is converted to all lower case when it is expanded

  `upper`

    for parameters whose value is converted to all upper case when it is expanded

  `readonly`

    for readonly parameters

  `tag`

    for tagged parameters

  `export`

    for exported parameters

  `unique`

    for arrays which keep only the first occurrence of duplicated values

  `hide`

    for parameters with the ‘hide’ flag

  `hideval`

    for parameters with the ‘hideval’ flag

  `special`

    for special parameters defined by the shell

`u`

  Expand only the first occurrence of each unique word.

`U`

  Convert all letters in the result to upper case.

`v`

  Used with `k`, substitute (as two consecutive words) both the key and the value of each associative array element. Used with subscripts, force values to be substituted even if the subscript form refers to indices or keys.

`V`

  Make any special characters in the resulting words visible.

`w`

  With `${#`name`}`, count words in arrays or strings; the `s` flag may be used to set a word delimiter.

`W`

  Similar to `w` with the difference that empty words between repeated delimiters are also counted.

`X`

  With this flag, parsing errors occurring with the `Q`, `e` and `#` flags or the pattern matching forms such as ‘`${`name`#`pattern`}`’ are reported. Without the flag, errors are silently ignored.

`z`

  Split the result of the expansion into words using shell parsing to find the words, i.e. taking into account any quoting in the value. Comments are not treated specially but as ordinary strings, similar to interactive shells with the `INTERACTIVE_COMMENTS` option unset (however, see the `Z` flag below for related options)

  Note that this is done very late, even later than the ‘`(s)`’ flag. So to access single words in the result use nested expansions as in ‘`${${(z)foo}[2]}`’. Likewise, to remove the quotes in the resulting words use ‘`${(Q)${(z)foo}}`’.

`0`

  Split the result of the expansion on null bytes. This is a shorthand for ‘`ps:\0:`’.

The following flags (except `p`) are followed by one or more arguments as shown. Any character, or the matching pairs ‘`(`...`)`’, ‘`{`...`}`’, ‘`[`...`]`’, or ‘`<`...`>`’, may be used in place of a colon as delimiters, but note that when a flag takes more than one argument, a matched pair of delimiters must surround each argument.

`p`

  Recognize the same escape sequences as the `print` builtin in string arguments to any of the flags described below that follow this argument.

  Alternatively, with this option string arguments may be in the form `$`var in which case the value of the variable is substituted. Note this form is strict; the string argument does not undergo general parameter expansion.

  For example,

      sep=:
      val=a:b:c
      print ${(ps.$sep.)val}

  splits the variable on a `:`.

`~`

  Strings inserted into the expansion by any of the flags below are to be treated as patterns. This applies to the string arguments of flags that follow `~` within the same set of parentheses. Compare with `~` outside parentheses, which forces the entire substituted string to be treated as a pattern. Hence, for example,

      [[ "?" = ${(~j.|.)array} ]]

  treats ‘`|`’ as a pattern and succeeds if and only if `$array` contains the string ‘`?`’ as an element. The `~` may be repeated to toggle the behaviour; its effect only lasts to the end of the parenthesised group.

`j:` string`:`

  Join the words of arrays together using string as a separator. Note that this occurs before field splitting by the `s:` string`:` flag or the `SH_WORD_SPLIT` option.

`l:` expr`::`string1`::`string2`:`

  Pad the resulting words on the left. Each word will be truncated if required and placed in a field expr characters wide.

  The arguments `:`string1`:` and `:`string2`:` are optional; neither, the first, or both may be given. Note that the same pairs of delimiters must be used for each of the three arguments. The space to the left will be filled with string1 (concatenated as often as needed) or spaces if string1 is not given. If both string1 and string2 are given, string2 is inserted once directly to the left of each word, truncated if necessary, before string1 is used to produce any remaining padding.

  If either of string1 or string2 is present but empty, i.e. there are two delimiters together at that point, the first character of `$IFS` is used instead.

  If the `MULTIBYTE` option is in effect, the flag `m` may also be given, in which case widths will be used for the calculation of padding; otherwise individual multibyte characters are treated as occupying one unit of width.

  If the `MULTIBYTE` option is not in effect, each byte in the string is treated as occupying one unit of width.

  Control characters are always assumed to be one unit wide; this allows the mechanism to be used for generating repetitions of control characters.

`m`

  Only useful together with one of the flags `l` or `r` or with the `#` length operator when the `MULTIBYTE` option is in effect. Use the character width reported by the system in calculating how much of the string it occupies or the overall length of the string. Most printable characters have a width of one unit, however certain Asian character sets and certain special effects use wider characters; combining characters have zero width. Non-printable characters are arbitrarily counted as zero width; how they would actually be displayed will vary.

  If the `m` is repeated, the character either counts zero (if it has zero width), else one. For printable character strings this has the effect of counting the number of glyphs (visibly separate characters), except for the case where combining characters themselves have non-zero width (true in certain alphabets).

`r:` expr`::`string1`::`string2`:`

  As `l`, but pad the words on the right and insert string2 immediately to the right of the string to be padded.

  Left and right padding may be used together. In this case the strategy is to apply left padding to the first half width of each of the resulting words, and right padding to the second half. If the string to be padded has odd width the extra padding is applied on the left.

`s:` string`:`

  Force field splitting at the separator string. Note that a string of two or more characters means that all of them must match in sequence; this differs from the treatment of two or more characters in the `IFS` parameter. See also the `=` flag and the `SH_WORD_SPLIT` option. An empty string may also be given in which case every character will be a separate element.

  For historical reasons, the usual behaviour that empty array elements are retained inside double quotes is disabled for arrays generated by splitting; hence the following:

      line="one::three"
      print -l "${(s.:.)line}"

  produces two lines of output for `one` and `three` and elides the empty field. To override this behaviour, supply the ‘`(@)`’ flag as well, i.e. `"${(@s.:.)line}"`.

`Z:` opts`:`

  As `z` but takes a combination of option letters between a following pair of delimiter characters. With no options the effect is identical to `z`. `(Z+c+)` causes comments to be parsed as a string and retained; any field in the resulting array beginning with an unquoted comment character is a comment. `(Z+C+)` causes comments to be parsed and removed. The rule for comments is standard: anything between a word starting with the third character of `$HISTCHARS`, default `#`, up to the next newline is a comment. `(Z+n+)` causes unquoted newlines to be treated as ordinary whitespace, else they are treated as if they are shell code delimiters and converted to semicolons. Options are combined within the same set of delimiters, e.g. `(Z+Cn+)`.

`_:` flags`:`

  The underscore (`_`) flag is reserved for future use. As of this revision of zsh, there are no valid flags; anything following an underscore, other than an empty pair of delimiters, is treated as an error, and the flag itself has no effect.

The following flags are meaningful with the `${`...`#`...`}` or `${`...`%`...`}` forms. The `S` and `I` flags may also be used with the `${`...`/`...`}` forms.

`S`

  Search substrings as well as beginnings or ends; with `#` start from the beginning and with `%` start from the end of the string. With substitution via `${`...`/`...`}` or `${`...`//`...`}`, specifies non-greedy matching, i.e. that the shortest instead of the longest match should be replaced.

`I:` expr`:`

  Search the exprth match (where expr evaluates to a number). This only applies when searching for substrings, either with the `S` flag, or with `${`...`/`...`}` (only the exprth match is substituted) or `${`...`//`...`}` (all matches from the exprth on are substituted). The default is to take the first match.

  The exprth match is counted such that there is either one or zero matches from each starting position in the string, although for global substitution matches overlapping previous replacements are ignored. With the `${`...`%`...`}` and `${`...`%%`...`}` forms, the starting position for the match moves backwards from the end as the index increases, while with the other forms it moves forward from the start.

  Hence with the string

      which switch is the right switch for Ipswich?

  substitutions of the form `${`(`SI:` N`:`)`string#w*ch}` as N increases from 1 will match and remove ‘`which`’, ‘`witch`’, ‘`witch`’ and ‘`wich`’; the form using ‘`##`’ will match and remove ‘`which switch is the right switch for Ipswich`’, ‘`witch is the right switch for Ipswich`’, ‘`witch for Ipswich`’ and ‘`wich`’. The form using ‘`%`’ will remove the same matches as for ‘`#`’, but in reverse order, and the form using ‘`%%`’ will remove the same matches as for ‘`##`’ in reverse order.

`B`

  Include the index of the beginning of the match in the result.

`E`

  Include the index of the end of the match in the result.

`M`

  Include the matched portion in the result.

`N`

  Include the length of the match in the result.

`R`

  Include the unmatched portion in the result (the Rest).

#### 14.3.2 Rules

Here is a summary of the rules for substitution; this assumes that braces are present around the substitution, i.e. `${`...`}`. Some particular examples are given below. Note that the Zsh Development Group accepts no responsibility for any brain damage which may occur during the reading of the following rules.

`1.` Nested substitution

  If multiple nested `${`...`}` forms are present, substitution is performed from the inside outwards. At each level, the substitution takes account of whether the current value is a scalar or an array, whether the whole substitution is in double quotes, and what flags are supplied to the current level of substitution, just as if the nested substitution were the outermost. The flags are not propagated up to enclosing substitutions; the nested substitution will return either a scalar or an array as determined by the flags, possibly adjusted for quoting. All the following steps take place where applicable at all levels of substitution.

  Note that, unless the ‘`(P)`’ flag is present, the flags and any subscripts apply directly to the value of the nested substitution; for example, the expansion `${${foo}}` behaves exactly the same as `${foo}`. When the ‘`(P)`’ flag is present in a nested substitution, the other substitution rules are applied to the value before it is interpreted as a name, so `${${(P)foo}}` may differ from `${(P)foo}`.

  At each nested level of substitution, the substituted words undergo all forms of single-word substitution (i.e. not filename generation), including command substitution, arithmetic expansion and filename expansion (i.e. leading `~` and `=`). Thus, for example, `${${:-=cat}:h}` expands to the directory where the `cat` program resides. (Explanation: the internal substitution has no parameter but a default value `=cat`, which is expanded by filename expansion to a full path; the outer substitution then applies the modifier `:h` and takes the directory part of the path.)

`2.` Internal parameter flags

  Any parameter flags set by one of the `typeset` family of commands, in particular the `-L`, `-R`, `-Z`, `-u` and `-l` options for padding and capitalization, are applied directly to the parameter value. Note these flags are options to the command, e.g. ‘`typeset -Z`’; they are not the same as the flags used within parameter substitutions.

  At the outermost level of substitution, the ‘`(P)`’ flag (rule `4.`) ignores these transformations and uses the unmodified value of the parameter as the name to be replaced. This is usually the desired behavior because padding may make the value syntactically illegal as a parameter name, but if capitalization changes are desired, use the `${${(P)foo}}` form (rule `25.`).

`3.` Parameter subscripting

  If the value is a raw parameter reference with a subscript, such as `${`var`[3]}`, the effect of subscripting is applied directly to the parameter. Subscripts are evaluated left to right; subsequent subscripts apply to the scalar or array value yielded by the previous subscript. Thus if `var` is an array, `${var[1][2]}` is the second character of the first word, but `${var[2,4][2]}` is the entire third word (the second word of the range of words two through four of the original array). Any number of subscripts may appear. Flags such as ‘`(k)`’ and ‘`(v)`’ which alter the result of subscripting are applied.

`4.` Parameter name replacement

  At the outermost level of nesting only, the ‘`(P)`’ flag is applied. This treats the value so far as a parameter name (which may include a subscript expression) and replaces that with the corresponding value. This replacement occurs later if the ‘`(P)`’ flag appears in a nested substitution.

  If the value so far names a parameter that has internal flags (rule `2.`), those internal flags are applied to the new value after replacement.

`5.` Double-quoted joining

  If the value after this process is an array, and the substitution appears in double quotes, and neither an ‘`(@)`’ flag nor a ‘`#`’ length operator is present at the current level, then words of the value are joined with the first character of the parameter `$IFS`, by default a space, between each word (single word arrays are not modified). If the ‘`(j)`’ flag is present, that is used for joining instead of `$IFS`.

`6.` Nested subscripting

  Any remaining subscripts (i.e. of a nested substitution) are evaluated at this point, based on whether the value is an array or a scalar. As with `3.`, multiple subscripts can appear. Note that `${foo[2,4][2]}` is thus equivalent to `${${foo[2,4]}[2]}` and also to `"${${(@)foo[2,4]}[2]}"` (the nested substitution returns an array in both cases), but not to `"${${foo[2,4]}[2]}"` (the nested substitution returns a scalar because of the quotes).

`7.` Modifiers

  Any modifiers, as specified by a trailing ‘`#`’, ‘`%`’, ‘`/`’ (possibly doubled) or by a set of modifiers of the form ‘`:...`’ (see Modifiers in History Expansion), are applied to the words of the value at this level.

`8.` Character evaluation

  Any ‘`(#)`’ flag is applied, evaluating the result so far numerically as a character.

`9.` Length

  Any initial ‘`#`’ modifier, i.e. in the form `${#`var`}`, is used to evaluate the length of the expression so far.

`10.` Forced joining

  If the ‘`(j)`’ flag is present, or no ‘`(j)`’ flag is present but the string is to be split as given by rule `11.`, and joining did not take place at rule `5.`, any words in the value are joined together using the given string or the first character of `$IFS` if none. Note that the ‘`(F)`’ flag implicitly supplies a string for joining in this manner.

`11.` Simple word splitting

  If one of the ‘`(s)`’ or ‘`(f)`’ flags are present, or the ‘`=`’ specifier was present (e.g. `${=`var`}`), the word is split on occurrences of the specified string, or (for `=` with neither of the two flags present) any of the characters in `$IFS`.

  If no ‘`(s)`’, ‘`(f)`’ or ‘`=`’ was given, but the word is not quoted and the option `SH_WORD_SPLIT` is set, the word is split on occurrences of any of the characters in `$IFS`. Note this step, too, takes place at all levels of a nested substitution.

`12.` Case modification

  Any case modification from one of the flags ‘`(L)`’, ‘`(U)`’ or ‘`(C)`’ is applied.

`13.` Escape sequence replacement

  First any replacements from the ‘`(g)`’ flag are performed, then any prompt-style formatting from the ‘`(%)`’ family of flags is applied.

`14.` Quote application

  Any quoting or unquoting using ‘`(q)`’ and ‘`(Q)`’ and related flags is applied.

`15.` Directory naming

  Any directory name substitution using ‘`(D)`’ flag is applied.

`16.` Visibility enhancement

  Any modifications to make characters visible using the ‘`(V)`’ flag are applied.

`17.` Lexical word splitting

  If the ’`(z)`’ flag or one of the forms of the ’`(Z)`’ flag is present, the word is split as if it were a shell command line, so that quotation marks and other metacharacters are used to decide what constitutes a word. Note this form of splitting is entirely distinct from that described by rule `11.`: it does not use `$IFS`, and does not cause forced joining.

`18.` Uniqueness

  If the result is an array and the ‘`(u)`’ flag was present, duplicate elements are removed from the array.

`19.` Ordering

  If the result is still an array and one of the ‘`(o)`’ or ‘`(O)`’ flags was present, the array is reordered.

`20.` `RC_EXPAND_PARAM`

  At this point the decision is made whether any resulting array elements are to be combined element by element with surrounding text, as given by either the `RC_EXPAND_PARAM` option or the ‘`^`’ flag.

`21.` Re-evaluation

  Any ‘`(e)`’ flag is applied to the value, forcing it to be re-examined for new parameter substitutions, but also for command and arithmetic substitutions.

`22.` Padding

  Any padding of the value by the ‘`(l.`fill`.)`’ or ‘`(r.`fill`.)`’ flags is applied.

`23.` Semantic joining

  In contexts where expansion semantics requires a single word to result, all words are rejoined with the first character of `IFS` between. So in ‘`${(P``)${(f``)lines}}`’ the value of `${lines}` is split at newlines, but then must be joined again before the ‘`(P)`’ flag can be applied.

  If a single word is not required, this rule is skipped.

`24.` Empty argument removal

  If the substitution does not appear in double quotes, any resulting zero-length argument, whether from a scalar or an element of an array, is elided from the list of arguments inserted into the command line.

  Strictly speaking, the removal happens later as the same happens with other forms of substitution; the point to note here is simply that it occurs after any of the above parameter operations.

`25.` Nested parameter name replacement

  If the ‘`(P)`’ flag is present and rule `4.` has not applied, the value so far is treated as a parameter name (which may include a subscript expression) and replaced with the corresponding value, with internal flags (rule `2.`) applied to the new value.

#### 14.3.3 Examples

The flag `f` is useful to split a double-quoted substitution line by line. For example, `${(f)"$(<`file`)"}` substitutes the contents of file divided so that each line is an element of the resulting array. Compare this with the effect of `$``(<`file`)` alone, which divides the file up by words, or the same inside double quotes, which makes the entire content of the file a single string.

The following illustrates the rules for nested parameter expansions. Suppose that `$foo` contains the array `(bar baz``)`:

`"${(@)${foo}[1]}"`

  This produces the result `b`. First, the inner substitution `"${foo}"`, which has no array (`@`) flag, produces a single word result `"bar baz"`. The outer substitution `"${(@)...[1]}"` detects that this is a scalar, so that (despite the ‘`(@)`’ flag) the subscript picks the first character.

`"${${(@)foo}[1]}"`

  This produces the result ‘`bar`’. In this case, the inner substitution `"${(@)foo}"` produces the array ‘`(bar baz``)`’. The outer substitution `"${...[1]}"` detects that this is an array and picks the first word. This is similar to the simple case `"${foo[1]}"`.

As an example of the rules for word splitting and joining, suppose `$foo` contains the array ‘`(ax1 bx1``)`’. Then

`${(s/x/)foo}`

  produces the words ‘`a`’, ‘`1 b`’ and ‘`1`’.

`${(j/x/s/x/)foo}`

  produces ‘`a`’, ‘`1`’, ‘`b`’ and ‘`1`’.

`${(s/x/)foo%%1*}`

  produces ‘`a`’ and ‘`b`’ (note the extra space). As substitution occurs before either joining or splitting, the operation first generates the modified array `(ax bx``)`, which is joined to give `"ax bx"`, and then split to give ‘`a`’, ‘`b`’ and ‘’. The final empty string will then be elided, as it is not in double quotes.

Next: Command Substitution, Previous: Process Substitution, Up: Expansion [Contents][Index]