<HTML> <HEAD> <!-- Created by texi2html 1.56k from ./zsh.texi on 26 June 2001 --> <TITLE>zsh - Expansion</TITLE> </HEAD> <BODY> Go to the <A HREF="zsh_1.html">first</A>, <A HREF="zsh_12.html">previous</A>, <A HREF="zsh_14.html">next</A>, <A HREF="zsh_29.html">last</A> section, <A HREF="zsh_toc.html">table of contents</A>. <P><HR><P> <H1><A NAME="SEC40" HREF="zsh_toc.html#TOC40">Expansion</A></H1> <P> <A NAME="IDX134"></A> <H2><A NAME="SEC41" HREF="zsh_toc.html#TOC41">Description</A></H2> <P> The types of expansions performed are <P> <DL COMPACT> <DT><EM>History Expansion</EM> <DD> <DT><EM>Alias Expansion</EM> <DD> <DT><EM>Process Substitution</EM> <DD> <DT><EM>Parameter Expansion</EM> <DD> <DT><EM>Command Substitution</EM> <DD> <DT><EM>Arithmetic Expansion</EM> <DD> <DT><EM>Brace Expansion</EM> <DD> <DT><EM>Filename Expansion</EM> <DD> <DT><EM>Filename Generation</EM> <DD> <DT> <DD> </DL> <P> Expansion is done in the above specified order in five steps. The first is <EM>history expansion</EM>, which is only performed in interactive shells. The next step is <EM>alias expansion</EM>, which is done right before the command line is parsed. They are followed by <EM>process substitution</EM>, <EM>parameter expansion</EM>, <EM>command substitution</EM>, <EM>arithmetic expansion</EM> and <EM>brace expansion</EM> which are performed in one step in left-to-right fashion. After these expansions, all unquoted occurrences of the characters `<TT>\</TT>', `<TT>'</TT>' and `<TT>"</TT>' are removed, and the result is subjected to <EM>filename expansion</EM> followed by <EM>filename generation</EM>. <P> If the <TT>SH_FILE_EXPANSION</TT> option is set, the order of expansion is modified for compatibility with <CITE>sh</CITE> and <CITE>ksh</CITE>. <EM>Filename expansion</EM> is performed immediately after <EM>alias expansion</EM>, preceding the set of five expansions mentioned above. <H2><A NAME="SEC42" HREF="zsh_toc.html#TOC42">History Expansion</A></H2> <P> <A NAME="IDX135"></A> <A NAME="IDX136"></A> <A NAME="IDX137"></A> History expansion allows you to use words from previous command lines in the command line you are typing. This simplifies spelling corrections and the repetition of complicated commands or arguments. <A NAME="IDX138"></A> Immediately before execution, each command is saved in the history list, the size of which is controlled by the <TT>HISTSIZE</TT> parameter. The one most recent command is always retained in any case. Each saved command in the history list is called a history <EM>event</EM> and is assigned a number, beginning with 1 (one) when the shell starts up. The history number that you may see in your prompt (see section <A HREF="zsh_12.html#SEC39">Prompt Expansion</A>) is the number that is to be assigned to the <EM>next</EM> command. <P> <H3><A NAME="SEC43" HREF="zsh_toc.html#TOC43">Overview</A></H3> <P> <A NAME="IDX139"></A> A history expansion begins with the first character of the <TT>histchars</TT> parameter, which is `<TT>!</TT>' by default, and may occur anywhere on the command line; history expansions do not nest. The `<TT>!</TT>' can be escaped with `<TT>\</TT>' or can be enclosed between a pair of single quotes (<TT>''</TT>) to suppress its special meaning. Double quotes will <EM>not</EM> work for this. Following this history character is an optional event designator (section <A HREF="zsh_13.html#SEC44">Event Designators</A>) and then an optional word designator (section <A HREF="zsh_13.html#SEC45">Word Designators</A>); if neither of these designators is present, no history expansion occurs. <P> Input lines containing history expansions are echoed after being expanded, but before any other expansions take place and before the command is executed. It is this expanded form that is recorded as the history event for later references. <P> By default, a history reference with no event designator refers to the same event as any preceding history reference on that command line; if it is the only history reference in a command, it refers to the previous command. <A NAME="IDX140"></A> However, if the option <TT>CSH_JUNKIE_HISTORY</TT> is set, then every history reference with no event specification <EM>always</EM> refers to the previous command. <P> For example, `<TT>!</TT>' is the event designator for the previous command, so `<TT>!!:1</TT>' always refers to the first word of the previous command, and `<TT>!!$</TT>' always refers to the last word of the previous command. With <TT>CSH_JUNKIE_HISTORY</TT> set, then `<TT>!:1</TT>' and `<TT>!$</TT>' function in the same manner as `<TT>!!:1</TT>' and `<TT>!!$</TT>', respectively. Conversely, if <TT>CSH_JUNKIE_HISTORY</TT> is unset, then `<TT>!:1</TT>' and `<TT>!$</TT>' refer to the first and last words, respectively, of the same event referenced by the nearest other history reference preceding them on the current command line, or to the previous command if there is no preceding reference. <P> The character sequence `<TT>^</TT><VAR>foo</VAR><TT>^</TT><VAR>bar</VAR>' (where `<TT>^</TT>' is actually the second character of the <TT>histchars</TT> parameter) repeats the last command, replacing the string <VAR>foo</VAR> with <VAR>bar</VAR>. More precisely, the sequence `<TT>^</TT><VAR>foo</VAR><TT>^</TT><VAR>bar</VAR><TT>^</TT>' is synonymous with `<TT>!!:s</TT><TT>^</TT><VAR>foo</VAR><TT>^</TT><VAR>bar</VAR><TT>^</TT>', hence other modifiers (see section <A HREF="zsh_13.html#SEC46">Modifiers</A>) may follow the final `<TT>^</TT>'. <P> If the shell encounters the character sequence `<TT>!"</TT>' in the input, the history mechanism is temporarily disabled until the current list (see section <A HREF="zsh_5.html#SEC18">Shell Grammar</A>) is fully parsed. The `<TT>!"</TT>' is removed from the input, and any subsequent `<TT>!</TT>' characters have no special significance. <P> <A NAME="IDX141"></A> A less convenient but more comprehensible form of command history support is provided by the <TT>fc</TT> builtin. <H3><A NAME="SEC44" HREF="zsh_toc.html#TOC44">Event Designators</A></H3> <P> <A NAME="IDX142"></A> <A NAME="IDX143"></A> An event designator is a reference to a command-line entry in the history list. In the list below, remember that the initial <TT>`!'</TT> in each item may be changed to another character by setting the <TT>histchars</TT> parameter. <P> <DL COMPACT> <DT><TT>!</TT> <DD> Start a history expansion, except when followed by a blank, newline, `<TT>=</TT>' or `<TT>(</TT>'. If followed immediately by a word designator (section <A HREF="zsh_13.html#SEC45">Word Designators</A>), this forms a history reference with no event designator (section <A HREF="zsh_13.html#SEC43">Overview</A>). <DT><TT>!!</TT> <DD> Refer to the previous command. By itself, this expansion repeats the previous command. <DT><TT>!</TT><VAR>n</VAR> <DD> Refer to command-line <VAR>n</VAR>. <DT><TT>!-</TT><VAR>n</VAR> <DD> Refer to the current command-line minus <VAR>n</VAR>. <DT><TT>!</TT><VAR>str</VAR> <DD> Refer to the most recent command starting with <VAR>str</VAR>. <DT><TT>!?</TT><VAR>str</VAR>[<TT>?</TT>] <DD> Refer to the most recent command containing <VAR>str</VAR>. The trailing `<TT>?</TT>' is necessary if this reference is to be followed by a modifier or followed by any text that is not to be considered part of <VAR>str</VAR>. <DT><TT>!#</TT> <DD> Refer to the current command line typed in so far. The line is treated as if it were complete up to and including the word before the one with the `<TT>!#</TT>' reference. <DT><TT>!{</TT>...<TT>}</TT> <DD> Insulate a history reference from adjacent characters (if necessary). </DL> <H3><A NAME="SEC45" HREF="zsh_toc.html#TOC45">Word Designators</A></H3> <P> <A NAME="IDX144"></A> <A NAME="IDX145"></A> A word designator indicates which word or words of a given command line are to be included in a history reference. A `<TT>:</TT>' usually separates the event specification from the word designator. It may be omitted only if the word designator begins with a `<TT>^</TT>', `<TT>$</TT>', `<TT>*</TT>', `<TT>-</TT>' or `<TT>%</TT>'. Word designators include: <P> <DL COMPACT> <DT><TT>0</TT> <DD> The first input word (command). <DT><VAR>n</VAR> <DD> The <VAR>n</VAR>th argument. <DT><TT>^</TT> <DD> The first argument. That is, <TT>1</TT>. <DT><TT>$</TT> <DD> The last argument. <DT><TT>%</TT> <DD> The word matched by (the most recent) <TT>?</TT><VAR>str</VAR> search. <DT><VAR>x</VAR><TT>-</TT><VAR>y</VAR> <DD> A range of words; <VAR>x</VAR> defaults to <TT>0</TT>. <DT><TT>*</TT> <DD> All the arguments, or a null value if there are none. <DT><VAR>x</VAR><TT>*</TT> <DD> Abbreviates `<VAR>x</VAR><TT>-$</TT>'. <DT><VAR>x</VAR><TT>-</TT> <DD> Like `<VAR>x</VAR><TT>*</TT>' but omitting word <TT>$</TT>. </DL> <P> Note that a `<TT>%</TT>' word designator works only when used in one of `<TT>!%</TT>', `<TT>!:%</TT>' or `<TT>!?</TT><VAR>str</VAR><TT>?:%</TT>', and only when used after a <TT>!?</TT> expansion (possibly in an earlier command). Anything else results in an error, although the error may not be the most obvious one. <H3><A NAME="SEC46" HREF="zsh_toc.html#TOC46">Modifiers</A></H3> <P> <A NAME="IDX146"></A> <A NAME="IDX147"></A> After the optional word designator, you can add a sequence of one or more of the following modifiers, each preceded by a `<TT>:</TT>'. These modifiers also work on the result of <EM>filename generation</EM> and <EM>parameter expansion</EM>, except where noted. <P> <DL COMPACT> <DT><TT>h</TT> <DD> Remove a trailing pathname component, leaving the head. This works like `<TT>dirname</TT>'. <DT><TT>r</TT> <DD> Remove a filename extension of the form `<TT>.</TT><VAR>xxx</VAR>', leaving the root name. <DT><TT>e</TT> <DD> Remove all but the extension. <DT><TT>t</TT> <DD> Remove all leading pathname components, leaving the tail. This works like `<TT>basename</TT>'. <DT><TT>p</TT> <DD> Print the new command but do not execute it. Only works with history expansion. <DT><TT>q</TT> <DD> Quote the substituted words, escaping further substitutions. Works with history expansion and parameter expansion, though for parameters it is only useful if the resulting text is to be re-evaluated such as by <TT>eval</TT>. <DT><TT>Q</TT> <DD> Remove one level of quotes from the substituted words. <DT><TT>x</TT> <DD> Like <TT>q</TT>, but break into words at each blank. Does not work with parameter expansion. <DT><TT>l</TT> <DD> Convert the words to all lowercase. <DT><TT>u</TT> <DD> Convert the words to all uppercase. <DT><TT>s/</TT><VAR>l</VAR><TT>/</TT><VAR>r</VAR>[<TT>/</TT>] <DD> Substitute <VAR>r</VAR> for <VAR>l</VAR> as described below. Unless preceded immediately by a <TT>g</TT>, with no colon between, the substitution is done only for the first string that matches <VAR>l</VAR>. For arrays and for filename generation, this applies to each word of the expanded text. <DT><TT>&</TT> <DD> Repeat the previous <TT>s</TT> substitution. Like <TT>s</TT>, may be preceded immediately by a <TT>g</TT>. In parameter expansion the <TT>&</TT> must appear inside braces, and in filename generation it must be quoted with a backslash. </DL> <P> The <TT>s/l/r/</TT> substitution works as follows. The left-hand side of substitutions are not regular expressions, but character strings. Any character can be used as the delimiter in place of `<TT>/</TT>'. A backslash quotes the delimiter character. The character `<TT>&</TT>', in the right-hand-side <VAR>r</VAR>, is replaced by the text from the left-hand-side <VAR>l</VAR>. The `<TT>&</TT>' can be quoted with a backslash. A null <VAR>l</VAR> uses the previous string either from the previous <VAR>l</VAR> or from the contextual scan string <VAR>s</VAR> from `<TT>!?</TT><VAR>s</VAR>'. You can omit the rightmost delimiter if a newline immediately follows <VAR>r</VAR>; the rightmost `<TT>?</TT>' in a context scan can similarly be omitted. Note the same record of the last <VAR>l</VAR> and <VAR>r</VAR> is maintained across all forms of expansion. <P> The following <TT>f</TT>, <TT>F</TT>, <TT>w</TT> and <TT>W</TT> modifiers work only with parameter expansion and filename generation. They are listed here to provide a single point of reference for all modifiers. <P> <DL COMPACT> <DT><TT>f</TT> <DD> Repeats the immediately (without a colon) following modifier until the resulting word doesn't change any more. <DT><TT>F:</TT><VAR>expr</VAR><TT>:</TT> <DD> Like <TT>f</TT>, but repeats only <VAR>n</VAR> times if the expression <VAR>expr</VAR> evaluates to <VAR>n</VAR>. Any character can be used instead of the `<TT>:</TT>'; if `<TT>(</TT>', `<TT>[</TT>', or `<TT>{</TT>' is used as the opening delimiter, the closing delimiter should be '<TT>)</TT>', `<TT>]</TT>', or `<TT>}</TT>', respectively. <DT><TT>w</TT> <DD> Makes the immediately following modifier work on each word in the string. <DT><TT>W:</TT><VAR>sep</VAR><TT>:</TT> <DD> Like <TT>w</TT> but words are considered to be the parts of the string that are separated by <VAR>sep</VAR>. Any character can be used instead of the `<TT>:</TT>'; opening parentheses are handled specially, see above. </DL> <H2><A NAME="SEC47" HREF="zsh_toc.html#TOC47">Process Substitution</A></H2> <P> <A NAME="IDX148"></A> <A NAME="IDX149"></A> Each command argument of the form `<TT><(</TT><VAR>list</VAR><TT>)</TT>', `<TT>>(</TT><VAR>list</VAR><TT>)</TT>' or `<TT>=(</TT><VAR>list</VAR><TT>)</TT>' is subject to process substitution. In the case of the <TT><</TT> or <TT>></TT> forms, the shell runs process <VAR>list</VAR> asynchronously. If the system supports the <TT>/dev/fd</TT> mechanism, the command argument is the name of the device file corresponding to a file descriptor; otherwise, if the system supports named pipes (FIFOs), the command argument will be a named pipe. If the form with <TT>></TT> is selected then writing on this special file will provide input for <VAR>list</VAR>. If <TT><</TT> is used, then the file passed as an argument will be connected to the output of the <VAR>list</VAR> process. For example, <P> <TT>paste <(cut -f1</TT> <VAR>file1</VAR><TT>) <(cut -f3</TT> <VAR>file2</VAR><TT>) |<BR> tee >(</TT><VAR>process1</VAR><TT>) >(</TT><VAR>process2</VAR><TT>) >/dev/null</TT> <P> cuts fields 1 and 3 from the files <VAR>file1</VAR> and <VAR>file2</VAR> respectively, pastes the results together, and sends it to the processes <VAR>process1</VAR> and <VAR>process2</VAR>. <P> Both the <TT>/dev/fd</TT> and the named pipe implementation have drawbacks. In the former case, some programmes may automatically close the file descriptor in question before examining the file on the command line, particularly if this is necessary for security reasons such as when the programme is running setuid. In the second case, if the programme does not actually open the file the subshell attempting to read from or write to the pipe will (in a typical implementation, different operating systems may have different behaviour) block for ever and have to be killed explicitly. In both cases, the shell actually supplies the information using a pipe, so that programmes that expect to lseek (see man page lseek(2)) on the file will not work. <P> Also note that the previous example can be more compactly and efficiently written (provided the <TT>MULTIOS</TT> option is set) as: <P> <TT>paste <(cut -f1</TT> <VAR>file1</VAR><TT>) <(cut -f3</TT> <VAR>file2</VAR><TT>) > >(</TT><VAR>process1</VAR><TT>) > >(</TT><VAR>process2</VAR><TT>)</TT> <P> The shell uses pipes instead of FIFOs to implement the latter two process substitutions in the above example. <P> If <TT>=</TT> is used, then the file passed as an argument will be the name of a temporary file containing the output of the <VAR>list</VAR> process. This may be used instead of the <TT><</TT> form for a program that expects to lseek (see man page lseek(2)) on the input file. <H2><A NAME="SEC48" HREF="zsh_toc.html#TOC48">Parameter Expansion</A></H2> <P> <A NAME="IDX150"></A> <A NAME="IDX151"></A> The character `<TT>$</TT>' is used to introduce parameter expansions. See section <A HREF="zsh_14.html#SEC64">Parameters</A> for a description of parameters, including arrays, associative arrays, and subscript notation to access individual array elements. <P> In the expansions discussed below that require a pattern, the form of the pattern is the same as that used for filename generation; see section <A HREF="zsh_13.html#SEC56">Filename Generation</A>. 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 file modifiers described in section <A HREF="zsh_13.html#SEC46">Modifiers</A> in section <A HREF="zsh_13.html#SEC42">History Expansion</A> can be applied: for example, <TT>${i:s/foo/bar/}</TT> performs string substitution on the expansion of parameter <TT>$i</TT>. <P> <DL COMPACT> <DT><TT>${</TT><VAR>name</VAR><TT>}</TT> <DD> The value, if any, of the parameter <VAR>name</VAR> 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 <VAR>name</VAR>. In addition, more complicated forms of substitution usually require the braces to be present; exceptions, which only apply if the option <TT>KSH_ARRAYS</TT> is not set, are a single subscript or any colon modifiers appearing after the name, or any of the characters `<TT>^</TT>', `<TT>=</TT>', `<TT>~</TT>', `<TT>#</TT>' or `<TT>+</TT>' appearing before the name, all of which work with or without braces. If <VAR>name</VAR> is an array parameter, and the <TT>KSH_ARRAYS</TT> option is not set, then the value of each element of <VAR>name</VAR> is substituted, one element per word. Otherwise, the expansion results in one word only; with <TT>KSH_ARRAYS</TT>, this is the first element of an array. No field splitting is done on the result unless the <TT>SH_WORD_SPLIT</TT> option is set. <DT><TT>${+</TT><VAR>name</VAR><TT>}</TT> <DD> If <VAR>name</VAR> is the name of a set parameter `<TT>1</TT>' is substituted, otherwise `<TT>0</TT>' is substituted. <DT><TT>${</TT><VAR>name</VAR><TT>:-</TT><VAR>word</VAR><TT>}</TT> <DD> If <VAR>name</VAR> is set and is non-null then substitute its value; otherwise substitute <VAR>word</VAR>. If <VAR>name</VAR> is missing, substitute <VAR>word</VAR>. <DT><TT>${</TT><VAR>name</VAR><TT>:=</TT><VAR>word</VAR><TT>}</TT> <DD> <DT><TT>${</TT><VAR>name</VAR><TT>::=</TT><VAR>word</VAR><TT>}</TT> <DD> In the first form, if <VAR>name</VAR> is unset or is null then set it to <VAR>word</VAR>; in the second form, unconditionally set <VAR>name</VAR> to <VAR>word</VAR>. In both forms, the value of the parameter is then substituted. <DT><TT>${</TT><VAR>name</VAR><TT>:?</TT><VAR>word</VAR><TT>}</TT> <DD> If <VAR>name</VAR> is set and is non-null then substitute its value; otherwise, print <VAR>word</VAR> and exit from the shell. Interactive shells instead return to the prompt. If <VAR>word</VAR> is omitted, then a standard message is printed. <DT><TT>${</TT><VAR>name</VAR><TT>:+</TT><VAR>word</VAR><TT>}</TT> <DD> If <VAR>name</VAR> is set and is non-null then substitute <VAR>word</VAR>; otherwise substitute nothing. </DL> <P> If the colon is omitted from one of the above expressions containing a colon, then the shell only checks whether <VAR>name</VAR> is set, not whether its value is null. <P> In the following expressions, when <VAR>name</VAR> is an array and the substitution is not quoted, or if the `<TT>(@)</TT>' flag or the <VAR>name</VAR><TT>[@]</TT> syntax is used, matching and replacement is performed on each array element separately. <P> <DL COMPACT> <DT><TT>${</TT><VAR>name</VAR><TT>#</TT><VAR>pattern</VAR><TT>}</TT> <DD> <DT><TT>${</TT><VAR>name</VAR><TT>##</TT><VAR>pattern</VAR><TT>}</TT> <DD> If the <VAR>pattern</VAR> matches the beginning of the value of <VAR>name</VAR>, then substitute the value of <VAR>name</VAR> with the matched portion deleted; otherwise, just substitute the value of <VAR>name</VAR>. In the first form, the smallest matching pattern is preferred; in the second form, the largest matching pattern is preferred. <DT><TT>${</TT><VAR>name</VAR><TT>%</TT><VAR>pattern</VAR><TT>}</TT> <DD> <DT><TT>${</TT><VAR>name</VAR><TT>%%</TT><VAR>pattern</VAR><TT>}</TT> <DD> If the <VAR>pattern</VAR> matches the end of the value of <VAR>name</VAR>, then substitute the value of <VAR>name</VAR> with the matched portion deleted; otherwise, just substitute the value of <VAR>name</VAR>. In the first form, the smallest matching pattern is preferred; in the second form, the largest matching pattern is preferred. <DT><TT>${</TT><VAR>name</VAR><TT>:#</TT><VAR>pattern</VAR><TT>}</TT> <DD> If the <VAR>pattern</VAR> matches the value of <VAR>name</VAR>, then substitute the empty string; otherwise, just substitute the value of <VAR>name</VAR>. If <VAR>name</VAR> is an array the matching array elements are removed (use the `<TT>(M)</TT>' flag to remove the non-matched elements). <DT><TT>${</TT><VAR>name</VAR><TT>/</TT><VAR>pattern</VAR><TT>/</TT><VAR>repl</VAR><TT>}</TT> <DD> <DT><TT>${</TT><VAR>name</VAR><TT>//</TT><VAR>pattern</VAR><TT>/</TT><VAR>repl</VAR><TT>}</TT> <DD> Replace the longest possible match of <VAR>pattern</VAR> in the expansion of parameter <VAR>name</VAR> by string <VAR>repl</VAR>. The first form replaces just the first occurrence, the second form all occurrences. Both <VAR>pattern</VAR> and <VAR>repl</VAR> are subject to double-quoted substitution, so that expressions like <TT>${name/$opat/$npat}</TT> will work, but note the usual rule that pattern characters in <TT>$opat</TT> are not treated specially unless either the option <TT>GLOB_SUBST</TT> is set, or <TT>$opat</TT> is instead substituted as <TT>${~opat}</TT>. The <VAR>pattern</VAR> may begin with a `<TT>#</TT>', in which case the <VAR>pattern</VAR> must match at the start of the string, or `<TT>%</TT>', in which case it must match at the end of the string. The <VAR>repl</VAR> may be an empty string, in which case the final `<TT>/</TT>' may also be omitted. To quote the final `<TT>/</TT>' in other cases it should be preceded by two backslashes (i.e., a quoted backslash); this is not necessary if the `<TT>/</TT>' occurs inside a substituted parameter. Note also that the `<TT>#</TT>' and `<TT>%</TT>' are not active if they occur inside a substituted parameter, even at the start. The first `<TT>/</TT>' may be preceded by a `<TT>:</TT>', in which case the match will only succeed if it matches the entire word. Note also the effect of the <TT>I</TT> and <TT>S</TT> parameter expansion flags below; however, the flags <TT>M</TT>, <TT>R</TT>, <TT>B</TT>, <TT>E</TT> and <TT>N</TT> are not useful. For example, <PRE> foo="twinkle twinkle little star" sub="t*e" rep="spy" print ${foo//${~sub}/$rep} print ${(S)foo//${~sub}/$rep} </PRE> Here, the `<TT>~</TT>' ensures that the text of <TT>$sub</TT> is treated as a pattern rather than a plain string. In the first case, the longest match for <TT>t*e</TT> is substituted and the result is `<TT>spy star</TT>', while in the second case, the shortest matches are taken and the result is `<TT>spy spy lispy star</TT>'. <DT><TT>${#</TT><VAR>spec</VAR><TT>}</TT> <DD> If <VAR>spec</VAR> is one of the above substitutions, substitute the length in characters of the result instead of the result itself. If <VAR>spec</VAR> is an array expression, substitute the number of elements of the result. Note that `<TT>^</TT>', `<TT>=</TT>', and `<TT>~</TT>', below, must appear to the left of `<TT>#</TT>' when these forms are combined. <DT><TT>${^</TT><VAR>spec</VAR><TT>}</TT> <DD> <A NAME="IDX152"></A> <A NAME="IDX153"></A> <A NAME="IDX154"></A> Turn on the <TT>RC_EXPAND_PARAM</TT> option for the evaluation of <VAR>spec</VAR>; if the `<TT>^</TT>' is doubled, turn it off. When this option is set, array expansions of the form <VAR>foo</VAR><TT>${</TT><VAR>xx</VAR><TT>}</TT><VAR>bar</VAR>, where the parameter <VAR>xx</VAR> is set to <TT>(</TT><VAR>a b c</VAR><TT>)</TT>, are substituted with `<VAR>fooabar foobbar foocbar</VAR>' instead of the default `<VAR>fooa b cbar</VAR>'. Internally, each such expansion is converted into the equivalent list for brace expansion. E.g., <TT>${^var}</TT> becomes <TT>{$var[1],$var[2],</TT>...<TT>}</TT>, and is processed as described in section <A HREF="zsh_13.html#SEC54">Brace Expansion</A> below. If word splitting is also in effect the <TT>$var[</TT><VAR>N</VAR><TT>]</TT> may themselves be split into different list elements. <DT><TT>${=</TT><VAR>spec</VAR><TT>}</TT> <DD> <A NAME="IDX155"></A> <A NAME="IDX156"></A> <A NAME="IDX157"></A> Perform word splitting using the rules for <TT>SH_WORD_SPLIT</TT> during the evaluation of <VAR>spec</VAR>, but regardless of whether the parameter appears in double quotes; if the `<TT>=</TT>' is doubled, turn it off. <A NAME="IDX158"></A> This forces parameter expansions to be split into separate words before substitution, using <TT>IFS</TT> as a delimiter. This is done by default in most other shells. Note that splitting is applied to <VAR>word</VAR> in the assignment forms of <VAR>spec</VAR> <EM>before</EM> the assignment to <VAR>name</VAR> is performed. This affects the result of array assignments with the <TT>A</TT> flag. <DT><TT>${~</TT><VAR>spec</VAR><TT>}</TT> <DD> <A NAME="IDX159"></A> Turn on the <TT>GLOB_SUBST</TT> option for the evaluation of <VAR>spec</VAR>; if the `<TT>~</TT>' 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 `<TT>=</TT>' and `<TT>!=</TT>' operators in conditions. </DL> <P> If a <TT>${</TT>...<TT>}</TT> type parameter expression or a <TT>$(</TT>...<TT>)</TT> type command substitution is used in place of <VAR>name</VAR> above, it is expanded first and the result is used as if it were the value of <VAR>name</VAR>. Thus it is possible to perform nested operations: <TT>${${foo#head}%tail}</TT> substitutes the value of <TT>$foo</TT> with both `<TT>head</TT>' and `<TT>tail</TT>' deleted. The form with <TT>$(</TT>...<TT>)</TT> is often useful in combination with the flags described next; see the examples below. Each <VAR>name</VAR> or nested <TT>${</TT>...<TT>}</TT> in a parameter expansion may also be followed by a subscript expression as described in section <A HREF="zsh_14.html#SEC66">Array Parameters</A>. <P> Note that double quotes may appear around nested expressions, in which case only the part inside is treated as quoted; for example, <TT>${(f)"$(foo)"}</TT> quotes the result of <TT>$(foo)</TT>, but the flag `<TT>(f)</TT>' (see below) is applied using the rules for unquoted expansions. Note further that quotes are themselves nested in this context; for example, in <TT>"${(@f)"$(foo)"}"</TT>, there are two sets of quotes, one surrounding the whole expression, the other (redundant) surrounding the <TT>$(foo)</TT> as before. <P> <H3><A NAME="SEC49" HREF="zsh_toc.html#TOC49">Parameter Expansion Flags</A></H3> <P> <A NAME="IDX160"></A> <A NAME="IDX161"></A> <A NAME="IDX162"></A> 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. Where arguments are valid, any character, or the matching pairs `<TT>(</TT>...<TT>)</TT>', `<TT>{</TT>...<TT>}</TT>', `<TT>[</TT>...<TT>]</TT>', or `<TT><</TT>...<TT>></TT>', may be used in place of the colon as delimiters. The following flags are supported: <P> <DL COMPACT> <DT><TT>A</TT> <DD> Create an array parameter with `<TT>${</TT>...<TT>=</TT>...<TT>}</TT>', `<TT>${</TT>...<TT>:=</TT>...<TT>}</TT>' or `<TT>${</TT>...<TT>::=</TT>...<TT>}</TT>'. If this flag is repeated (as in `<TT>AA</TT>'), create an associative array parameter. Assignment is made before sorting or padding. The <VAR>name</VAR> part may be a subscripted range for ordinary arrays; the <VAR>word</VAR> part <EM>must</EM> be converted to an array, for example by using `<TT>${(AA)=</TT><VAR>name</VAR><TT>=</TT>...<TT>}</TT>' to activate word splitting, when creating an associative array. <DT><TT>@</TT> <DD> In double quotes, array elements are put into separate words. E.g., `<TT>"${(@)foo}"</TT>' is equivalent to `<TT>"${foo[@]}"</TT>' and `<TT>"${(@)foo[1,2]}"</TT>' is the same as `<TT>"$foo[1]" "$foo[2]"</TT>'. <DT><TT>e</TT> <DD> Perform <EM>parameter expansion</EM>, <EM>command substitution</EM> and <EM>arithmetic expansion</EM> on the result. Such expansions can be nested but too deep recursion may have unpredictable effects. <DT><TT>P</TT> <DD> This forces the value of the parameter <VAR>name</VAR> to be interpreted as a further parameter name, whose value will be used where appropriate. 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 `<TT>foo=bar</TT>' and `<TT>bar=baz</TT>', the strings <TT>${(P)foo}</TT>, <TT>${(P)${foo}}</TT>, and <TT>${(P)$(echo bar)}</TT> will be expanded to `<TT>baz</TT>'. <DT><TT>o</TT> <DD> Sort the resulting words in ascending order. <DT><TT>O</TT> <DD> Sort the resulting words in descending order. <DT><TT>i</TT> <DD> With <TT>o</TT> or <TT>O</TT>, sort case-independently. <DT><TT>L</TT> <DD> Convert all letters in the result to lower case. <DT><TT>U</TT> <DD> Convert all letters in the result to upper case. <DT><TT>C</TT> <DD> Capitalize the resulting words. `Words' in this case refers to sequences of alphanumeric characters separated by non-alphanumerics, <EM>not</EM> to words that result from field splitting. <DT><TT>V</TT> <DD> Make any special characters in the resulting words visible. <DT><TT>q</TT> <DD> Quote the resulting words with backslashes. 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. If it is given four times, the words are quoted in single quotes preceded by a <TT>$</TT>. <DT><TT>Q</TT> <DD> Remove one level of quotes from the resulting words. <DT><TT>%</TT> <DD> Expand all <TT>%</TT> escapes in the resulting words in the same way as in prompts (see section <A HREF="zsh_12.html#SEC39">Prompt Expansion</A>). If this flag is given twice, full prompt expansion is done on the resulting words, depending on the setting of the <TT>PROMPT_PERCENT</TT>, <TT>PROMPT_SUBST</TT> and <TT>PROMPT_BANG</TT> options. <DT><TT>X</TT> <DD> With this flag parsing errors occurring with the <TT>Q</TT> and <TT>e</TT> flags or the pattern matching forms such as `<TT>${</TT><VAR>name</VAR><TT>#</TT><VAR>pattern</VAR><TT>}</TT>' are reported. Without the flag they are silently ignored. <DT><TT>c</TT> <DD> With <TT>${#</TT><VAR>name</VAR><TT>}</TT>, count the total number of characters in an array, as if the elements were concatenated with spaces between them. <DT><TT>w</TT> <DD> With <TT>${#</TT><VAR>name</VAR><TT>}</TT>, count words in arrays or strings; the <TT>s</TT> flag may be used to set a word delimiter. <DT><TT>W</TT> <DD> Similar to <TT>w</TT> with the difference that empty words between repeated delimiters are also counted. <DT><TT>k</TT> <DD> If <VAR>name</VAR> refers to an associative array, substitute the <EM>keys</EM> (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. <DT><TT>v</TT> <DD> Used with <TT>k</TT>, 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. <DT><TT>p</TT> <DD> Recognize the same escape sequences as the <TT>print</TT> builtin in string arguments to any of the flags described below. <DT><TT>l:</TT><VAR>expr</VAR><TT>::</TT><VAR>string1</VAR><TT>::</TT><VAR>string2</VAR><TT>:</TT> <DD> Pad the resulting words on the left. Each word will be truncated if required and placed in a field <VAR>expr</VAR> characters wide. The space to the left will be filled with <VAR>string1</VAR> (concatenated as often as needed) or spaces if <VAR>string1</VAR> is not given. If both <VAR>string1</VAR> and <VAR>string2</VAR> are given, this string is inserted once directly to the left of each word, before padding. <DT><TT>r:</TT><VAR>expr</VAR><TT>::</TT><VAR>string1</VAR><TT>::</TT><VAR>string2</VAR><TT>:</TT> <DD> As <TT>l</TT>, but pad the words on the right and insert <VAR>string2</VAR> on the right. <DT><TT>j:</TT><VAR>string</VAR><TT>:</TT> <DD> Join the words of arrays together using <VAR>string</VAR> as a separator. <A NAME="IDX163"></A> Note that this occurs before field splitting by the <TT>SH_WORD_SPLIT</TT> option. <DT><TT>F</TT> <DD> Join the words of arrays together using newline as a separator. This is a shorthand for `<TT>pj:\n:</TT>'. <DT><TT>s:</TT><VAR>string</VAR><TT>:</TT> <DD> Force field splitting (see the option <TT>SH_WORD_SPLIT</TT>) at the separator <VAR>string</VAR>. Splitting only occurs in places where an array value is valid. <DT><TT>f</TT> <DD> Split the result of the expansion to lines. This is a shorthand for `<TT>ps:\n:</TT>'. <DT><TT>z</TT> <DD> 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. Note that this is done very late, as for the `<TT>(s)</TT>' flag. So to access single words in the result, one has to use nested expansions as in `<TT>${${(z)foo}[2]}</TT>'. Likewise, to remove the quotes in the resulting words one would do: `<TT>${(Q)${(z)foo}}</TT>'. <DT><TT>t</TT> <DD> 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 (`<TT>-</TT>'). The first keyword in the string describes the main type, it can be one of `<TT>scalar</TT>', `<TT>array</TT>', `<TT>integer</TT>', `<TT>float</TT>' or `<TT>association</TT>'. The other keywords describe the type in more detail: <DL COMPACT> <DT><TT>local</TT> <DD> for local parameters <DT><TT>left</TT> <DD> for left justified parameters <DT><TT>right_blanks</TT> <DD> for right justified parameters with leading blanks <DT><TT>right_zeros</TT> <DD> for right justified parameters with leading zeros <DT><TT>lower</TT> <DD> for parameters whose value is converted to all lower case when it is expanded <DT><TT>upper</TT> <DD> for parameters whose value is converted to all upper case when it is expanded <DT><TT>readonly</TT> <DD> for readonly parameters <DT><TT>tag</TT> <DD> for tagged parameters <DT><TT>export</TT> <DD> for exported parameters <DT><TT>unique</TT> <DD> for arrays which keep only the first occurrence of duplicated values <DT><TT>hide</TT> <DD> for parameters with the `hide' flag <DT><TT>special</TT> <DD> for special parameters defined by the shell </DL> </DL> <P> The following flags are meaningful with the <TT>${</TT>...<TT>#</TT>...<TT>}</TT> or <TT>${</TT>...<TT>%</TT>...<TT>}</TT> forms. The <TT>S</TT> and <TT>I</TT> flags may also be used with the <TT>${</TT>...<TT>/</TT>...<TT>}</TT> forms. <P> <DL COMPACT> <DT><TT>S</TT> <DD> Search substrings as well as beginnings or ends; with <TT>#</TT> start from the beginning and with <TT>%</TT> start from the end of the string. With substitution via <TT>${</TT>...<TT>/</TT>...<TT>}</TT> or <TT>${</TT>...<TT>//</TT>...<TT>}</TT>, specifies that the shortest instead of the longest match should be replaced. <DT><TT>I:</TT><VAR>expr</VAR><TT>:</TT> <DD> Search the <VAR>expr</VAR>th match (where <VAR>expr</VAR> evaluates to a number). This only applies when searching for substrings, either with the <TT>S</TT> flag, or with <TT>${</TT>...<TT>/</TT>...<TT>}</TT> (only the <VAR>expr</VAR>th match is substituted) or <TT>${</TT>...<TT>//</TT>...<TT>}</TT> (all matches from the <VAR>expr</VAR>th on are substituted). The <VAR>expr</VAR>th 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. <DT><TT>M</TT> <DD> Include the matched portion in the result. <DT><TT>R</TT> <DD> Include the unmatched portion in the result (the <EM>R</EM>est). <DT><TT>B</TT> <DD> Include the index of the beginning of the match in the result. <DT><TT>E</TT> <DD> Include the index of the end of the match in the result. <DT><TT>N</TT> <DD> Include the length of the match in the result. </DL> <P> <H3><A NAME="SEC50" HREF="zsh_toc.html#TOC50">Rules</A></H3> <P> <P> Here is a summary of the rules for substitution; this assumes that braces are present around the substitution, i.e. <TT>${...}</TT>. Some particular examples are given below. Note that the Zsh Development Group accepts <EM>no responsibility</EM> for any brain damage which may occur during the reading of the following rules. <P> <DL COMPACT> <DT><TT>1.</TT> <EM>Nested Substitution</EM> <DD> If multiple nested <TT>${...}</TT> 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 `<TT>(P)</TT>' flag is present, the flags and any subscripts apply directly to the value of the nested substitution; for example, the expansion <TT>${${foo}}</TT> behaves exactly the same as <TT>${foo}</TT>. <DT><TT>2.</TT> <EM>Parameter Subscripting</EM> <DD> If the value is a raw parameter reference with a subscript, such as <TT>${</TT><VAR>var</VAR><TT>[3]}</TT>, 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 <TT>var</TT> is an array, <TT>${var[1][2]}</TT> is the second character of the first word, but <TT>${var[2,4][2]}</TT> 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. <DT><TT>3.</TT> <EM>Parameter Name Replacement</EM> <DD> The effect of any <TT>(P)</TT> flag, which treats the value so far as a parameter name and replaces it with the corresponding value, is applied. <DT><TT>4.</TT> <EM>Double-Quoted Joining</EM> <DD> If the value after this process is an array, and the substitution appears in double quotes, and no <TT>(@)</TT> flag is present at the current level, the words of the value are joined with the first character of the parameter <TT>$IFS</TT>, by default a space, between each word (single word arrays are not modified). If the <TT>(j)</TT> flag is present, that is used for joining instead of <TT>$IFS</TT>. <DT><TT>5.</TT> <EM>Nested Subscripting</EM> <DD> 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 <TT>2.</TT>, multiple subscripts can appear. Note that <TT>${foo[2,4][2]}</TT> is thus equivalent to <TT>${${foo[2,4]}[2]}</TT> and also to <TT>"${${(@)foo[2,4]}[2]}"</TT> (the nested substitution returns an array in both cases), but not to <TT>"${${foo[2,4]}[2]}"</TT> (the nested substitution returns a scalar because of the quotes). <DT><TT>6.</TT> <EM>Modifiers</EM> <DD> Any modifiers, as specified by a trailing `<TT>#</TT>', `<TT>%</TT>', `<TT>/</TT>' (possibly doubled) or by a set of modifiers of the form <TT>:...</TT> (see section <A HREF="zsh_13.html#SEC46">Modifiers</A> in section <A HREF="zsh_13.html#SEC42">History Expansion</A>), are applied to the words of the value at this level. <DT><TT>7.</TT> <EM>Forced Joining</EM> <DD> If the `<TT>(j)</TT>' flag is present, or no `<TT>(j)</TT>' flag is present but the string is to be split as given by rules <TT>8.</TT> or <TT>9.</TT>, and joining did not take place at step <TT>4.</TT>, any words in the value are joined together using the given string or the first character of <TT>$IFS</TT> if none. Note that the `<TT>(F)</TT>' flag implicitly supplies a string for joining in this manner. <DT><TT>8.</TT> <EM>Forced Splitting</EM> <DD> If one of the `<TT>(s)</TT>', `<TT>(f)</TT>' or `<TT>(z)</TT>' flags are present, or the `<TT>=</TT>' specifier was present (e.g. <TT>${=</TT><VAR>var</VAR><TT>}</TT>), the word is split on occurrences of the specified string, or (for <TT>=</TT> with neither of the two flags present) any of the characters in <TT>$IFS</TT>. <DT><TT>9.</TT> <EM>Shell Word Splitting</EM> <DD> If no `<TT>(s)</TT>', `<TT>(f)</TT>' or `<TT>=</TT>' was given, but the word is not quoted and the option <TT>SH_WORD_SPLIT</TT> is set, the word is split on occurrences of any of the characters in <TT>$IFS</TT>. Note this step, too, takes place at all levels of a nested substitution. <DT><TT>10.</TT> <EM>Re-Evaluation</EM> <DD> Any `<TT>(e)</TT>' flag is applied to the value, forcing it to be re-examined for new parameter substitutions, but also for command and arithmetic substitutions. <DT><TT>11.</TT> <EM>Padding</EM> <DD> Any padding of the value by the `<TT>(l.</TT><VAR>fill</VAR><TT>.)</TT>' or `<TT>(r.</TT><VAR>fill</VAR><TT>.)</TT>' flags is applied. </DL> <P> <H3><A NAME="SEC51" HREF="zsh_toc.html#TOC51">Examples</A></H3> <P> The flag <TT>f</TT> is useful to split a double-quoted substitution line by line. For example, <TT>${(f)"$(<</TT><VAR>file</VAR><TT>)"}</TT> substitutes the contents of <VAR>file</VAR> divided so that each line is an element of the resulting array. Compare this with the effect of <TT>$</TT><TT>(<</TT><VAR>file</VAR><TT>)</TT> 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. <P> The following illustrates the rules for nested parameter expansions. Suppose that <TT>$foo</TT> contains the array <TT>(bar baz</TT><TT>)</TT>: <P> <DL COMPACT> <DT><TT>"${(@)${foo}[1]}"</TT> <DD> This produces the result <TT>b</TT>. First, the inner substitution <TT>"${foo}"</TT>, which has no array (<TT>@</TT>) flag, produces a single word result <TT>"bar baz"</TT>. The outer substitution <TT>"${(@)...[1]}"</TT> detects that this is a scalar, so that (despite the `<TT>(@)</TT>' flag) the subscript picks the first character. <DT><TT>"${${(@)foo}[1]}"</TT> <DD> This produces the result `<TT>bar</TT>'. In this case, the inner substitution <TT>"${(@)foo}"</TT> produces the array `<TT>(bar baz</TT><TT>)</TT>'. The outer substitution <TT>"${...[1]}"</TT> detects that this is an array and picks the first word. This is similar to the simple case <TT>"${foo[1]}"</TT>. </DL> <P> As an example of the rules for word splitting and joining, suppose <TT>$foo</TT> contains the array `<TT>(ax1 bx1</TT><TT>)</TT>'. Then <P> <DL COMPACT> <DT><TT>${(s/x/)foo}</TT> <DD> produces the words `<TT>a</TT>', `<TT>1 b</TT>' and `<TT>1</TT>'. <DT><TT>${(j/x/s/x/)foo}</TT> <DD> produces `<TT>a</TT>', `<TT>1</TT>', `<TT>b</TT>' and `<TT>1</TT>'. <DT><TT>${(s/x/)foo%%1*}</TT> <DD> produces `<TT>a</TT>' and `<TT> b</TT>' (note the extra space). As substitution occurs before either joining or splitting, the operation first generates the modified array <TT>(ax bx</TT><TT>)</TT>, which is joined to give <TT>"ax bx"</TT>, and then split to give `<TT>a</TT>', `<TT> b</TT>' and `'. The final empty string will then be elided, as it is not in double quotes. </DL> <P> <H2><A NAME="SEC52" HREF="zsh_toc.html#TOC52">Command Substitution</A></H2> <P> <A NAME="IDX164"></A> <A NAME="IDX165"></A> A command enclosed in parentheses preceded by a dollar sign, like `<TT>$(</TT>...<TT>)</TT>', or quoted with grave accents, like `<TT>`</TT>...<TT>`</TT>', is replaced with its standard output, with any trailing newlines deleted. If the substitution is not enclosed in double quotes, the output is broken into words using the <TT>IFS</TT> parameter. <A NAME="IDX166"></A> The substitution `<TT>$(cat</TT> <VAR>foo</VAR><TT>)</TT>' may be replaced by the equivalent but faster `<TT>$(<</TT><VAR>foo</VAR><TT>)</TT>'. In either case, if the option <TT>GLOB_SUBST</TT> is set, the output is eligible for filename generation. <H2><A NAME="SEC53" HREF="zsh_toc.html#TOC53">Arithmetic Expansion</A></H2> <P> <A NAME="IDX167"></A> <A NAME="IDX168"></A> A string of the form `<TT>$[</TT><VAR>exp</VAR><TT>]</TT>' or `<TT>$((</TT><VAR>exp</VAR><TT>))</TT>' is substituted with the value of the arithmetic expression <VAR>exp</VAR>. <VAR>exp</VAR> is subjected to <EM>parameter expansion</EM>, <EM>command substitution</EM> and <EM>arithmetic expansion</EM> before it is evaluated. See section <A HREF="zsh_10.html#SEC37">Arithmetic Evaluation</A>. <H2><A NAME="SEC54" HREF="zsh_toc.html#TOC54">Brace Expansion</A></H2> <P> <A NAME="IDX169"></A> <A NAME="IDX170"></A> A string of the form `<VAR>foo</VAR><TT>{</TT><VAR>xx</VAR><TT>,</TT><VAR>yy</VAR><TT>,</TT><VAR>zz</VAR><TT>}</TT><VAR>bar</VAR>' is expanded to the individual words `<VAR>fooxxbar</VAR>', `<VAR>fooyybar</VAR>' and `<VAR>foozzbar</VAR>'. Left-to-right order is preserved. This construct may be nested. Commas may be quoted in order to include them literally in a word. <P> An expression of the form `<TT>{</TT><VAR>n1</VAR><TT>..</TT><VAR>n2</VAR><TT>}</TT>', where <VAR>n1</VAR> and <VAR>n2</VAR> are integers, is expanded to every number between <VAR>n1</VAR> and <VAR>n2</VAR> inclusive. If either number begins with a zero, all the resulting numbers will be padded with leading zeroes to that minimum width. If the numbers are in decreasing order the resulting sequence will also be in decreasing order. <P> If a brace expression matches none of the above forms, it is left unchanged, unless the <TT>BRACE_CCL</TT> option is set. <A NAME="IDX171"></A> In that case, it is expanded to a sorted list of the individual characters between the braces, in the manner of a search set. `<TT>-</TT>' is treated specially as in a search set, but `<TT>^</TT>' or `<TT>!</TT>' as the first character is treated normally. <P> Note that brace expansion is not part of filename generation (globbing); an expression such as <TT>*/{foo,bar}</TT> is split into two separate words <TT>*/foo</TT> and <TT>*/bar</TT> before filename generation takes place. In particular, note that this is liable to produce a `no match' error if <EM>either</EM> of the two expressions does not match; this is to be contrasted with <TT>*/(foo|bar)</TT>, which is treated as a single pattern but otherwise has similar effects. <H2><A NAME="SEC55" HREF="zsh_toc.html#TOC55">Filename Expansion</A></H2> <P> <A NAME="IDX172"></A> <A NAME="IDX173"></A> Each word is checked to see if it begins with an unquoted `<TT>~</TT>'. If it does, then the word up to a `<TT>/</TT>', or the end of the word if there is no `<TT>/</TT>', is checked to see if it can be substituted in one of the ways described here. If so, then the `<TT>~</TT>' and the checked portion are replaced with the appropriate substitute value. <P> A `<TT>~</TT>' by itself is replaced by the value of <TT>$HOME</TT>. A `<TT>~</TT>' followed by a `<TT>+</TT>' or a `<TT>-</TT>' is replaced by the value of <TT>$PWD</TT> or <TT>$OLDPWD</TT>, respectively. <P> A `<TT>~</TT>' followed by a number is replaced by the directory at that position in the directory stack. `<TT>~0</TT>' is equivalent to `<TT>~+</TT>', and `<TT>~1</TT>' is the top of the stack. `<TT>~+</TT>' followed by a number is replaced by the directory at that position in the directory stack. `<TT>~+0</TT>' is equivalent to `<TT>~+</TT>', and `<TT>~+1</TT>' is the top of the stack. `<TT>~-</TT>' followed by a number is replaced by the directory that many positions from the bottom of the stack. `<TT>~-0</TT>' is the bottom of the stack. <A NAME="IDX174"></A> The <TT>PUSHD_MINUS</TT> option exchanges the effects of `<TT>~+</TT>' and `<TT>~-</TT>' where they are followed by a number. <P> <A NAME="IDX175"></A> <A NAME="IDX176"></A> A `<TT>~</TT>' followed by anything not already covered is looked up as a named directory, and replaced by the value of that named directory if found. Named directories are typically home directories for users on the system. They may also be defined if the text after the `<TT>~</TT>' is the name of a string shell parameter whose value begins with a `<TT>/</TT>'. It is also possible to define directory names using the <TT>-d</TT> option to the <TT>hash</TT> builtin. <P> In certain circumstances (in prompts, for instance), when the shell prints a path, the path is checked to see if it has a named directory as its prefix. If so, then the prefix portion is replaced with a `<TT>~</TT>' followed by the name of the directory. The shortest way of referring to the directory is used, with ties broken in favour of using a named directory, except when the directory is <TT>/</TT> itself. The parameters <TT>$PWD</TT> and <TT>$OLDPWD</TT> are never abbreviated in this fashion. <P> If a word begins with an unquoted `<TT>=</TT>' and the <TT>EQUALS</TT> option is set, the remainder of the word is taken as the name of a command or alias. If a command exists by that name, the word is replaced by the full pathname of the command. If an alias exists by that name, the word is replaced with the text of the alias. <P> Filename expansion is performed on the right hand side of a parameter assignment, including those appearing after commands of the <TT>typeset</TT> family. In this case, the right hand side will be treated as a colon-separated list in the manner of the <TT>PATH</TT> parameter, so that a `<TT>~</TT>' or an `<TT>=</TT>' following a `<TT>:</TT>' is eligible for expansion. All such behaviour can be disabled by quoting the `<TT>~</TT>', the `<TT>=</TT>', or the whole expression (but not simply the colon); the <TT>EQUALS</TT> option is also respected. <P> If the option <TT>MAGIC_EQUAL_SUBST</TT> is set, any unquoted shell argument in the form `<VAR>identifier</VAR><TT>=</TT><VAR>expression</VAR>' becomes eligible for file expansion as described in the previous paragraph. Quoting the first `<TT>=</TT>' also inhibits this. <H2><A NAME="SEC56" HREF="zsh_toc.html#TOC56">Filename Generation</A></H2> <P> <A NAME="IDX177"></A> <A NAME="IDX178"></A> If a word contains an unquoted instance of one of the characters `<TT>*</TT>', `<TT>(</TT>', `<TT>|</TT>', `<TT><</TT>', `<TT>[</TT>', or `<TT>?</TT>', it is regarded as a pattern for filename generation, unless the <TT>GLOB</TT> option is unset. <A NAME="IDX179"></A> If the <TT>EXTENDED_GLOB</TT> option is set, <A NAME="IDX180"></A> the `<TT>^</TT>' and `<TT>#</TT>' characters also denote a pattern; otherwise they are not treated specially by the shell. <P> The word is replaced with a list of sorted filenames that match the pattern. If no matching pattern is found, the shell gives an error message, unless the <TT>NULL_GLOB</TT> option is set, <A NAME="IDX181"></A> in which case the word is deleted; or unless the <TT>NOMATCH</TT> option is unset, in which case the word is left unchanged. <A NAME="IDX182"></A> <P> In filename generation, the character `<TT>/</TT>' must be matched explicitly; also, a `<TT>.</TT>' must be matched explicitly at the beginning of a pattern or after a `<TT>/</TT>', unless the <TT>GLOB_DOTS</TT> option is set. <A NAME="IDX183"></A> No filename generation pattern matches the files `<TT>.</TT>' or `<TT>..</TT>'. In other instances of pattern matching, the `<TT>/</TT>' and `<TT>.</TT>' are not treated specially. <H3><A NAME="SEC57" HREF="zsh_toc.html#TOC57">Glob Operators</A></H3> <P> <DL COMPACT> <DT><TT>*</TT> <DD> Matches any string, including the null string. <DT><TT>?</TT> <DD> Matches any character. <DT><TT>[</TT>...<TT>]</TT> <DD> Matches any of the enclosed characters. Ranges of characters can be specified by separating two characters by a `<TT>-</TT>'. A `<TT>-</TT>' or `<TT>]</TT>' may be matched by including it as the first character in the list. <A NAME="IDX184"></A> There are also several named classes of characters, in the form `<TT>[:</TT><VAR>name</VAR><TT>:]</TT>' with the following meanings: `<TT>[:alnum:]</TT>' alphanumeric, `<TT>[:alpha:]</TT>' alphabetic, `<TT>[:blank:]</TT>' space or tab, `<TT>[:cntrl:]</TT>' control character, `<TT>[:digit:]</TT>' decimal digit, `<TT>[:graph:]</TT>' printable character except whitespace, `<TT>[:lower:]</TT>' lowercase letter, `<TT>[:print:]</TT>' printable character, `<TT>[:punct:]</TT>' printable character neither alphanumeric nor whitespace, `<TT>[:space:]</TT>' whitespace character, `<TT>[:upper:]</TT>' uppercase letter, `<TT>[:xdigit:]</TT>' hexadecimal digit. These use the macros provided by the operating system to test for the given character combinations, including any modifications due to local language settings: see man page ctype(3). Note that the square brackets are additional to those enclosing the whole set of characters, so to test for a single alphanumeric character you need `<TT>[[:alnum:]]</TT>'. Named character sets can be used alongside other types, e.g. `<TT>[[:alpha:]0-9]</TT>'. <DT><TT>[^</TT>...<TT>]</TT> <DD> <DT><TT>[!</TT>...<TT>]</TT> <DD> Like <TT>[</TT>...<TT>]</TT>, except that it matches any character which is not in the given set. <DT><TT><</TT>[<VAR>x</VAR>]<TT>-</TT>[<VAR>y</VAR>]<TT>></TT> <DD> Matches any number in the range <VAR>x</VAR> to <VAR>y</VAR>, inclusive. Either of the numbers may be omitted to make the range open-ended; hence `<TT><-></TT>' matches any number. To match individual digits, the <TT>[</TT>...<TT>]</TT> form is more efficient. Be careful when using other wildcards adjacent to patterns of this form; for example, <TT><0-9>*</TT> will actually match any number whatsoever at the start of the string, since the `<TT><0-9></TT>' will match the first digit, and the `<TT>*</TT>' will match any others. This is a trap for the unwary, but is in fact an inevitable consequence of the rule that the longest possible match always succeeds. Expressions such as `<TT><0-9>[^[:digit:]]*</TT>' can be used instead. <DT><TT>(</TT>...<TT>)</TT> <DD> Matches the enclosed pattern. This is used for grouping. If the <TT>KSH_GLOB</TT> option is set, then a `<TT>@</TT>', `<TT>*</TT>', `<TT>+</TT>', `<TT>?</TT>' or `<TT>!</TT>' immediately preceding the `<TT>(</TT>' is treated specially, as detailed below. The option <TT>SH_GLOB</TT> prevents bare parentheses from being used in this way, though the <TT>KSH_GLOB</TT> option is still available. Note that grouping cannot extend over multiple directories: it is an error to have a `<TT>/</TT>' within a group (this only applies for patterns used in filename generation). There is one exception: a group of the form <TT>(</TT><VAR>pat</VAR><TT>/)#</TT> appearing as a complete path segment can match a sequence of directories. For example, <TT>foo/(a*/)#bar</TT> matches <TT>foo/bar</TT>, <TT>foo/any/bar</TT>, <TT>foo/any/anyother/bar</TT>, and so on. <DT><VAR>x</VAR><TT>|</TT><VAR>y</VAR> <DD> Matches either <VAR>x</VAR> or <VAR>y</VAR>. This operator has lower precedence than any other. The `<TT>|</TT>' character must be within parentheses, to avoid interpretation as a pipeline. <DT><TT>^</TT><VAR>x</VAR> <DD> (Requires <TT>EXTENDED_GLOB</TT> to be set.) Matches anything except the pattern <VAR>x</VAR>. This has a higher precedence than `<TT>/</TT>', so `<TT>^foo/bar</TT>' will search directories in `<TT>.</TT>' except `<TT>./foo</TT>' for a file named `<TT>bar</TT>'. <DT><VAR>x</VAR><TT>~</TT><VAR>y</VAR> <DD> (Requires <TT>EXTENDED_GLOB</TT> to be set.) Match anything that matches the pattern <VAR>x</VAR> but does not match <VAR>y</VAR>. This has lower precedence than any operator except `<TT>|</TT>', so `<TT>*/*~foo/bar</TT>' will search for all files in all directories in `<TT>.</TT>' and then exclude `<TT>foo/bar</TT>' if there was such a match. Multiple patterns can be excluded by `<VAR>foo</VAR><TT>~</TT><VAR>bar</VAR><TT>~</TT><VAR>baz</VAR>'. In the exclusion pattern (<VAR>y</VAR>), `<TT>/</TT>' and `<TT>.</TT>' are not treated specially the way they usually are in globbing. <DT><VAR>x</VAR><TT>#</TT> <DD> (Requires <TT>EXTENDED_GLOB</TT> to be set.) Matches zero or more occurrences of the pattern <VAR>x</VAR>. This operator has high precedence; `<TT>12#</TT>' is equivalent to `<TT>1(2#)</TT>', rather than `<TT>(12)#</TT>'. It is an error for an unquoted `<TT>#</TT>' to follow something which cannot be repeated; this includes an empty string, a pattern already followed by `<TT>##</TT>', or parentheses when part of a <TT>KSH_GLOB</TT> pattern (for example, `<TT>!(</TT><VAR>foo</VAR><TT>)#</TT>' is invalid and must be replaced by `<TT>*(!(</TT><VAR>foo</VAR><TT>))</TT>'). <DT><VAR>x</VAR><TT>##</TT> <DD> (Requires <TT>EXTENDED_GLOB</TT> to be set.) Matches one or more occurrences of the pattern <VAR>x</VAR>. This operator has high precedence; `<TT>12##</TT>' is equivalent to `<TT>1(2##)</TT>', rather than `<TT>(12)##</TT>'. No more than two active `<TT>#</TT>' characters may appear together. </DL> <H3><A NAME="SEC58" HREF="zsh_toc.html#TOC58">ksh-like Glob Operators</A></H3> <P> <A NAME="IDX185"></A> If the <TT>KSH_GLOB</TT> option is set, the effects of parentheses can be modified by a preceding `<TT>@</TT>', `<TT>*</TT>', `<TT>+</TT>', `<TT>?</TT>' or `<TT>!</TT>'. This character need not be unquoted to have special effects, but the `<TT>(</TT>' must be. <P> <DL COMPACT> <DT><TT>@(</TT>...<TT>)</TT> <DD> Match the pattern in the parentheses. (Like `<TT>(</TT>...<TT>)</TT>'.) <DT><TT>*(</TT>...<TT>)</TT> <DD> Match any number of occurrences. (Like `<TT>(</TT>...<TT>)#</TT>'.) <DT><TT>+(</TT>...<TT>)</TT> <DD> Match at least one occurrence. (Like `<TT>(</TT>...<TT>)##</TT>'.) <DT><TT>?(</TT>...<TT>)</TT> <DD> Match zero or one occurrence. (Like `<TT>(|</TT>...<TT>)</TT>'.) <DT><TT>!(</TT>...<TT>)</TT> <DD> Match anything but the expression in parentheses. (Like `<TT>(^(</TT>...<TT>))</TT>'.) </DL> <H3><A NAME="SEC59" HREF="zsh_toc.html#TOC59">Precedence</A></H3> <P> <A NAME="IDX186"></A> The precedence of the operators given above is (highest) `<TT>^</TT>', `<TT>/</TT>', `<TT>~</TT>', `<TT>|</TT>' (lowest); the remaining operators are simply treated from left to right as part of a string, with `<TT>#</TT>' and `<TT>##</TT>' applying to the shortest possible preceding unit (i.e. a character, `<TT>?</TT>', `<TT>[</TT>...<TT>]</TT>', `<TT><</TT>...<TT>></TT>', or a parenthesised expression). As mentioned above, a `<TT>/</TT>' used as a directory separator may not appear inside parentheses, while a `<TT>|</TT>' must do so; in patterns used in other contexts than filename generation (for example, in <TT>case</TT> statements and tests within `<TT>[[</TT>...<TT>]]</TT>'), a `<TT>/</TT>' is not special; and `<TT>/</TT>' is also not special after a `<TT>~</TT>' appearing outside parentheses in a filename pattern. <H3><A NAME="SEC60" HREF="zsh_toc.html#TOC60">Globbing Flags</A></H3> <P> There are various flags which affect any text to their right up to the end of the enclosing group or to the end of the pattern; they require the <TT>EXTENDED_GLOB</TT> option. All take the form <TT>(#</TT><VAR>X</VAR><TT>)</TT> where <VAR>X</VAR> may have one of the following forms: <P> <DL COMPACT> <DT>i <DD> Case insensitive: upper or lower case characters in the pattern match upper or lower case characters. <DT>l <DD> Lower case characters in the pattern match upper or lower case characters; upper case characters in the pattern still only match upper case characters. <DT>I <DD> Case sensitive: locally negates the effect of <TT>i</TT> or <TT>l</TT> from that point on. <DT>b <DD> Activate backreferences for parenthesised groups in the pattern; this does not work in filename generation. When a pattern with a set of active parentheses is matched, the strings matched by the groups are stored in the array <TT>$match</TT>, the indices of the beginning of the matched parentheses in the array <TT>$mbegin</TT>, and the indices of the end in the array <TT>$mend</TT>, with the first element of each array corresponding to the first parenthesised group, and so on. These arrays are not otherwise special to the shell. The indices use the same convention as does parameter substitution, so that elements of <TT>$mend</TT> and <TT>$mbegin</TT> may be used in subscripts; the <TT>KSH_ARRAYS</TT> option is respected. Sets of globbing flags are not considered parenthesised groups; only the first nine active parentheses can be referenced. For example, <PRE> foo="a string with a message" if [[ $foo = (a|an)' '(#b)(*)' '* ]]; then print ${foo[$mbegin[1],$mend[1]]} fi </PRE> prints `<TT>string with a</TT>'. Note that the first parenthesis is before the <TT>(#b)</TT> and does not create a backreference. Backreferences work with all forms of pattern matching other than filename generation, but note that when performing matches on an entire array, such as <TT>${</TT><VAR>array</VAR><TT>#</TT><VAR>pattern</VAR><TT>}</TT>, or a global substitution, such as <TT>${</TT><VAR>param</VAR><TT>//</TT><VAR>pat</VAR><TT>/</TT><VAR>repl</VAR><TT>}</TT>, only the data for the last match remains available. In the case of global replacements this may still be useful. See the example for the <TT>m</TT> flag below. The numbering of backreferences strictly follows the order of the opening parentheses from left to right in the pattern string, although sets of parentheses may be nested. There are special rules for parentheses followed by `<TT>#</TT>' or `<TT>##</TT>'. Only the last match of the parenthesis is remembered: for example, in `<TT>[[ abab = (#b)([ab])# ]]</TT>', only the final `<TT>b</TT>' is stored in <TT>match[1]</TT>. Thus extra parentheses may be necessary to match the complete segment: for example, use `<TT>X((ab|cd)#)Y</TT>' to match a whole string of either `<TT>ab</TT>' or `<TT>cd</TT>' between `<TT>X</TT>' and `<TT>Y</TT>', using the value of <TT>$match[1]</TT> rather than <TT>$match[2]</TT>. If the match fails none of the parameters is altered, so in some cases it may be necessary to initialise them beforehand. If some of the backreferences fail to match -- which happens if they are in an alternate branch which fails to match, or if they are followed by <TT>#</TT> and matched zero times -- then the matched string is set to the empty string, and the start and end indices are set to -1. Pattern matching with backreferences is slightly slower than without. <DT>B <DD> Deactivate backreferences, negating the effect of the <TT>b</TT> flag from that point on. <DT>m <DD> Set references to the match data for the entire string matched; this is similar to backreferencing and does not work in filename generation. The flag must be in effect at the end of the pattern, i.e. not local to a group. The parameters <TT>$MATCH</TT>, <TT>$MBEGIN</TT> and <TT>$MEND</TT> will be set to the string matched and to the indices of the beginning and end of the string, respectively. This is most useful in parameter substitutions, as otherwise the string matched is obvious. For example, <PRE> arr=(veldt jynx grimps waqf zho buck) print ${arr//(#m)[aeiou]/${(U)MATCH}} </PRE> forces all the matches (i.e. all vowels) into uppercase, printing `<TT>vEldt jynx grImps wAqf zhO bUck</TT>'. Unlike backreferences, there is no speed penalty for using match references, other than the extra substitutions required for the replacement strings in cases such as the example shown. <DT>M <DD> Deactivate the <TT>m</TT> flag, hence no references to match data will be created. <DT><TT>a</TT><VAR>num</VAR> <DD> Approximate matching: <VAR>num</VAR> errors are allowed in the string matched by the pattern. The rules for this are described in the next subsection. <DT><TT>s</TT>, <TT>e</TT> <DD> Unlike the other flags, these have only a local effect, and each must appear on its own: `<TT>(#s)</TT>' and `<TT>(#e)</TT>' are the only valid forms. The `<TT>(#s)</TT>' flag succeeds only at the start of the test string, and the `<TT>(#e)</TT>' flag succeeds only at the end of the test string; they correspond to `<TT>^</TT>' and `<TT>$</TT>' in standard regular expressions. They are useful for matching path segments in patterns other than those in filename generation (where path segments are in any case treated separately). For example, `<TT>*((#s)|/)test((#e)|/)*</TT>' matches a path segment `<TT>test</TT>' in any of the following strings: <TT>test</TT>, <TT>test/at/start</TT>, <TT>at/end/test</TT>, <TT>in/test/middle</TT>. Another use is in parameter substitution; for example `<TT>${array/(#s)A*Z(#e)}</TT>' will remove only elements of an array which match the complete pattern `<TT>A*Z</TT>'. There are other ways of performing many operations of this type, however the combination of the substitution operations `<TT>/</TT>' and `<TT>//</TT>' with the `<TT>(#s)</TT>' and `<TT>(#e)</TT>' flags provides a single simple and memorable method. Note that assertions of the form `<TT>(^(#s))</TT>' also work, i.e. match anywhere except at the start of the string, although this actually means `anything except a zero-length portion at the start of the string'; you need to use `<TT>(""~(#s))</TT>' to match a zero-length portion of the string not at the start. </DL> <P> For example, the test string <TT>fooxx</TT> can be matched by the pattern <TT>(#i</TT><TT>)FOOXX</TT>, but not by <TT>(#l</TT><TT>)FOOXX</TT>, <TT>(#i</TT><TT>)FOO</TT><TT>(#I</TT><TT>)XX</TT> or <TT>((#i</TT><TT>)FOOX</TT><TT>)X</TT>. The string <TT>(#ia2</TT><TT>)readme</TT> specifies case-insensitive matching of <TT>readme</TT> with up to two errors. <P> When using the ksh syntax for grouping both <TT>KSH_GLOB</TT> and <TT>EXTENDED_GLOB</TT> must be set and the left parenthesis should be preceded by <TT>@</TT>. Note also that the flags do not affect letters inside <TT>[...]</TT> groups, in other words <TT>(#i</TT><TT>)[a-z]</TT> still matches only lowercase letters. Finally, note that when examining whole paths case-insensitively every directory must be searched for all files which match, so that a pattern of the form <TT>(#i</TT><TT>)/foo/bar/...</TT> is potentially slow. <P> <H3><A NAME="SEC61" HREF="zsh_toc.html#TOC61">Approximate Matching</A></H3> <P> When matching approximately, the shell keeps a count of the errors found, which cannot exceed the number specified in the <TT>(#a</TT><VAR>num</VAR><TT>)</TT> flags. Four types of error are recognised: <P> <DL COMPACT> <DT>1. <DD> Different characters, as in <TT>fooxbar</TT> and <TT>fooybar</TT>. <DT>2. <DD> Transposition of characters, as in <TT>banana</TT> and <TT>abnana</TT>. <DT>3. <DD> A character missing in the target string, as with the pattern <TT>road</TT> and target string <TT>rod</TT>. <DT>4. <DD> An extra character appearing in the target string, as with <TT>stove</TT> and <TT>strove</TT>. </DL> <P> Thus, the pattern <TT>(#a3</TT><TT>)abcd</TT> matches <TT>dcba</TT>, with the errors occurring by using the first rule twice and the second once, grouping the string as <TT>[d][cb][a]</TT> and <TT>[a][bc][d]</TT>. <P> Non-literal parts of the pattern must match exactly, including characters in character ranges: hence <TT>(#a1</TT><TT>)???</TT> matches strings of length four, by applying rule 4 to an empty part of the pattern, but not strings of length two, since all the <TT>?</TT> must match. Other characters which must match exactly are initial dots in filenames (unless the <TT>GLOB_DOTS</TT> option is set), and all slashes in filenames, so that <TT>a/bc</TT> is two errors from <TT>ab/c</TT> (the slash cannot be transposed with another character). Similarly, errors are counted separately for non-contiguous strings in the pattern, so that <TT>(ab|cd</TT><TT>)ef</TT> is two errors from <TT>aebf</TT>. <P> When using exclusion via the <TT>~</TT> operator, approximate matching is treated entirely separately for the excluded part and must be activated separately. Thus, <TT>(#a1</TT><TT>)README~READ_ME</TT> matches <TT>READ.ME</TT> but not <TT>READ_ME</TT>, as the trailing <TT>READ_ME</TT> is matched without approximation. However, <TT>(#a1</TT><TT>)README~(#a1</TT><TT>)READ_ME</TT> does not match any pattern of the form <TT>READ</TT><VAR>?</VAR><TT>ME</TT> as all such forms are now excluded. <P> Apart from exclusions, there is only one overall error count; however, the maximum errors allowed may be altered locally, and this can be delimited by grouping. For example, <TT>(#a1</TT><TT>)cat</TT><TT>((#a0</TT><TT>)dog</TT><TT>)fox</TT> allows one error in total, which may not occur in the <TT>dog</TT> section, and the pattern <TT>(#a1</TT><TT>)cat</TT><TT>(#a0</TT><TT>)dog</TT><TT>(#a1</TT><TT>)fox</TT> is equivalent. Note that the point at which an error is first found is the crucial one for establishing whether to use approximation; for example, <TT>(#a1)abc(#a0)xyz</TT> will not match <TT>abcdxyz</TT>, because the error occurs at the `<TT>x</TT>', where approximation is turned off. <P> Entire path segments may be matched approximately, so that `<TT>(#a1)/foo/d/is/available/at/the/bar</TT>' allows one error in any path segment. This is much less efficient than without the <TT>(#a1)</TT>, however, since every directory in the path must be scanned for a possible approximate match. It is best to place the <TT>(#a1)</TT> after any path segments which are known to be correct. <P> <H3><A NAME="SEC62" HREF="zsh_toc.html#TOC62">Recursive Globbing</A></H3> <P> A pathname component of the form `<TT>(</TT><VAR>foo</VAR><TT>/)#</TT>' matches a path consisting of zero or more directories matching the pattern <VAR>foo</VAR>. <P> As a shorthand, `<TT>**/</TT>' is equivalent to `<TT>(*/)#</TT>'; note that this therefore matches files in the current directory as well as subdirectories. Thus: <P> <PRE> ls (*/)#bar </PRE> <P> or <P> <PRE> ls **/bar </PRE> <P> does a recursive directory search for files named `<TT>bar</TT>' (potentially including the file `<TT>bar</TT>' in the current directory). This form does not follow symbolic links; the alternative form `<TT>***/</TT>' does, but is otherwise identical. Neither of these can be combined with other forms of globbing within the same path segment; in that case, the `<TT>*</TT>' operators revert to their usual effect. <H3><A NAME="SEC63" HREF="zsh_toc.html#TOC63">Glob Qualifiers</A></H3> <P> <A NAME="IDX187"></A> <A NAME="IDX188"></A> Patterns used for filename generation may end in a list of qualifiers enclosed in parentheses. The qualifiers specify which filenames that otherwise match the given pattern will be inserted in the argument list. <P> <A NAME="IDX189"></A> If the option <TT>BARE_GLOB_QUAL</TT> is set, then a trailing set of parentheses containing no `<TT>|</TT>' or `<TT>(</TT>' characters (or `<TT>~</TT>' if it is special) is taken as a set of glob qualifiers. A glob subexpression that would normally be taken as glob qualifiers, for example `<TT>(^x)</TT>', can be forced to be treated as part of the glob pattern by doubling the parentheses, in this case producing `<TT>((^x))</TT>'. <P> A qualifier may be any one of the following: <P> <DL COMPACT> <DT><TT>/</TT> <DD> directories <DT><TT>.</TT> <DD> plain files <DT><TT>@</TT> <DD> symbolic links <DT><TT>=</TT> <DD> sockets <DT><TT>p</TT> <DD> named pipes (FIFOs) <DT><TT>*</TT> <DD> executable plain files (0100) <DT><TT>%</TT> <DD> device files (character or block special) <DT><TT>%b</TT> <DD> block special files <DT><TT>%c</TT> <DD> character special files <DT><TT>r</TT> <DD> owner-readable files (0400) <DT><TT>w</TT> <DD> owner-writable files (0200) <DT><TT>x</TT> <DD> owner-executable files (0100) <DT><TT>A</TT> <DD> group-readable files (0040) <DT><TT>I</TT> <DD> group-writable files (0020) <DT><TT>E</TT> <DD> group-executable files (0010) <DT><TT>R</TT> <DD> world-readable files (0004) <DT><TT>W</TT> <DD> world-writable files (0002) <DT><TT>X</TT> <DD> world-executable files (0001) <DT><TT>s</TT> <DD> setuid files (04000) <DT><TT>S</TT> <DD> setgid files (02000) <DT><TT>t</TT> <DD> files with the sticky bit (01000) <DT><TT>f</TT><VAR>spec</VAR> <DD> files with access rights matching <VAR>spec</VAR>. This <VAR>spec</VAR> may be a octal number optionally preceded by a `<TT>=</TT>', a `<TT>+</TT>', or a `<TT>-</TT>'. If none of these characters is given, the behavior is the same as for `<TT>=</TT>'. The octal number describes the mode bits to be expected, if combined with a `<TT>=</TT>', the value given must match the file-modes exactly, with a `<TT>+</TT>', at least the bits in the given number must be set in the file-modes, and with a `<TT>-</TT>', the bits in the number must not be set. Giving a `<TT>?</TT>' instead of a octal digit anywhere in the number ensures that the corresponding bits in the file-modes are not checked, this is only useful in combination with `<TT>=</TT>'. If the qualifier `<TT>f</TT>' is followed by any other character anything up to the next matching character (`<TT>[</TT>', `<TT>{</TT>', and `<TT><</TT>' match `<TT>]</TT>', `<TT>}</TT>', and `<TT>></TT>' respectively, any other character matches itself) is taken as a list of comma-separated <VAR>sub-spec</VAR>s. Each <VAR>sub-spec</VAR> may be either a octal number as described above or a list of any of the characters `<TT>u</TT>', `<TT>g</TT>', `<TT>o</TT>', and `<TT>a</TT>', followed by a `<TT>=</TT>', a `<TT>+</TT>', or a `<TT>-</TT>', followed by a list of any of the characters `<TT>r</TT>', `<TT>w</TT>', `<TT>x</TT>', `<TT>s</TT>', and `<TT>t</TT>', or a octal digit. The first list of characters specify which access rights are to be checked. If a `<TT>u</TT>' is given, those for the owner of the file are used, if a `<TT>g</TT>' is given, those of the group are checked, a `<TT>o</TT>' means to test those of other users, and the `<TT>a</TT>' says to test all three groups. The `<TT>=</TT>', `<TT>+</TT>', and `<TT>-</TT>' again says how the modes are to be checked and have the same meaning as described for the first form above. The second list of characters finally says which access rights are to be expected: `<TT>r</TT>' for read access, `<TT>w</TT>' for write access, `<TT>x</TT>' for the right to execute the file (or to search a directory), `<TT>s</TT>' for the setuid and setgid bits, and `<TT>t</TT>' for the sticky bit. Thus, `<TT>*(f70?)</TT>' gives the files for which the owner has read, write, and execute permission, and for which other group members have no rights, independent of the permissions for other users. The pattern `<TT>*(f-100)</TT>' gives all files for which the owner does not have execute permission, and `<TT>*(f:gu+w,o-rx:)</TT>' gives the files for which the owner and the other members of the group have at least write permission, and for which other users don't have read or execute permission. <DT><TT>e</TT><VAR>string</VAR> <DD> The <VAR>string</VAR> will be executed as shell code. The filename will be included in the list if and only if the code returns a zero status (usually the status of the last command). The first character after the `<TT>e</TT>' will be used as a separator and anything up to the next matching separator will be taken as the <VAR>string</VAR>; `<TT>[</TT>', `<TT>{</TT>', and `<TT><</TT>' match `<TT>]</TT>', `<TT>}</TT>', and `<TT>></TT>', respectively, while any other character matches itself. Note that expansions must be quoted in the <VAR>string</VAR> to prevent them from being expanded before globbing is done. <A NAME="IDX190"></A> <A NAME="IDX191"></A> During the execution of <VAR>string</VAR> the filename currently being tested is available in the parameter <TT>REPLY</TT>; the parameter may be altered to a string to be inserted into the list instead of the original filename. In addition, the parameter <TT>reply</TT> may be set to an array or a string, which overrides the value of <TT>REPLY</TT>. If set to an array, the latter is inserted into the command line word by word. For example, suppose a directory contains a single file `<TT>lonely</TT>'. Then the expression `<TT>*(e:'reply=(${REPLY}{1,2})':)</TT>' will cause the words `<TT>lonely1 lonely2</TT>' to be inserted into the command line. Note the quotation marks. <DT><TT>d</TT><VAR>dev</VAR> <DD> files on the device <VAR>dev</VAR> <DT><TT>l</TT>[<TT>-</TT>|<TT>+</TT>]<VAR>ct</VAR> <DD> files having a link count less than <VAR>ct</VAR> (<TT>-</TT>), greater than <VAR>ct</VAR> (<TT>+</TT>), or equal to <VAR>ct</VAR> <DT><TT>U</TT> <DD> files owned by the effective user ID <DT><TT>G</TT> <DD> files owned by the effective group ID <DT><TT>u</TT><VAR>id</VAR> <DD> files owned by user ID <VAR>id</VAR> if it is a number, if not, than the character after the `<TT>u</TT>' will be used as a separator and the string between it and the next matching separator (`<TT>[</TT>', `<TT>{</TT>', and `<TT><</TT>' match `<TT>]</TT>', `<TT>}</TT>', and `<TT>></TT>' respectively, any other character matches itself) will be taken as a user name, and the user ID of this user will be taken (e.g. `<TT>u:foo:</TT>' or `<TT>u[foo]</TT>' for user `<TT>foo</TT>') <DT><TT>g</TT><VAR>id</VAR> <DD> like <TT>u</TT><VAR>id</VAR> but with group IDs or names <DT><TT>a</TT>[<TT>Mwhms</TT>][<TT>-</TT>|<TT>+</TT>]<VAR>n</VAR> <DD> files accessed exactly <VAR>n</VAR> days ago. Files accessed within the last <VAR>n</VAR> days are selected using a negative value for <VAR>n</VAR> (<TT>-</TT><VAR>n</VAR>). Files accessed more than <VAR>n</VAR> days ago are selected by a positive <VAR>n</VAR> value (<TT>+</TT><VAR>n</VAR>). Optional unit specifiers `<TT>M</TT>', `<TT>w</TT>', `<TT>h</TT>', `<TT>m</TT>' or `<TT>s</TT>' (e.g. `<TT>ah5</TT>') cause the check to be performed with months (of 30 days), weeks, hours, minutes or seconds instead of days, respectively. For instance, `<TT>echo *(ah-5)</TT>' would echo files accessed within the last five hours. <DT><TT>m</TT>[<TT>Mwhms</TT>][<TT>-</TT>|<TT>+</TT>]<VAR>n</VAR> <DD> like the file access qualifier, except that it uses the file modification time. <DT><TT>c</TT>[<TT>Mwhms</TT>][<TT>-</TT>|<TT>+</TT>]<VAR>n</VAR> <DD> like the file access qualifier, except that it uses the file inode change time. <DT><TT>L</TT>[<TT>+</TT>|<TT>-</TT>]<VAR>n</VAR> <DD> files less than <VAR>n</VAR> bytes (<TT>-</TT>), more than <VAR>n</VAR> bytes (<TT>+</TT>), or exactly <VAR>n</VAR> bytes in length. If this flag is directly followed by a `<TT>k</TT>' (`<TT>K</TT>'), `<TT>m</TT>' (`<TT>M</TT>'), or `<TT>p</TT>' (`<TT>P</TT>') (e.g. `<TT>Lk-50</TT>') the check is performed with kilobytes, megabytes, or blocks (of 512 bytes) instead. <DT><TT>^</TT> <DD> negates all qualifiers following it <DT><TT>-</TT> <DD> toggles between making the qualifiers work on symbolic links (the default) and the files they point to <DT><TT>M</TT> <DD> sets the <TT>MARK_DIRS</TT> option for the current pattern <A NAME="IDX192"></A> <DT><TT>T</TT> <DD> appends a trailing qualifier mark to the filenames, analogous to the <TT>LIST_TYPES</TT> option, for the current pattern (overrides <TT>M</TT>) <DT><TT>N</TT> <DD> sets the <TT>NULL_GLOB</TT> option for the current pattern <A NAME="IDX193"></A> <DT><TT>D</TT> <DD> sets the <TT>GLOB_DOTS</TT> option for the current pattern <A NAME="IDX194"></A> <DT><TT>n</TT> <DD> sets the <TT>NUMERIC_GLOB_SORT</TT> option for the current pattern <A NAME="IDX195"></A> <DT><TT>o</TT><VAR>c</VAR> <DD> specifies how the names of the files should be sorted. If <VAR>c</VAR> is <TT>n</TT> they are sorted by name (the default); if it is <TT>L</TT> they are sorted depending on the size (length) of the files; if <TT>l</TT> they are sorted by the number of links; if <TT>a</TT>, <TT>m</TT>, or <TT>c</TT> they are sorted by the time of the last access, modification, or inode change respectively; if <VAR>d</VAR>, files in subdirectories appear before those in the current directory at each level of the search -- this is best combined with other criteria, for example `<TT>odon</TT>' to sort on names for files within the same directory. Note that <TT>a</TT>, <TT>m</TT>, and <TT>c</TT> compare the age against the current time, hence the first name in the list is the youngest file. Also note that the modifiers <TT>^</TT> and <TT>-</TT> are used, so `<TT>*(^-oL)</TT>' gives a list of all files sorted by file size in descending order, following any symbolic links. <DT><TT>O</TT><VAR>c</VAR> <DD> like `<TT>o</TT>', but sorts in descending order; i.e. `<TT>*(^oc)</TT>' is the same as `<TT>*(Oc)</TT>' and `<TT>*(^Oc)</TT>' is the same as `<TT>*(oc)</TT>'; `<TT>Od</TT>' puts files in the current directory before those in subdirectories at each level of the search. <DT><TT>[</TT><VAR>beg</VAR>[<TT>,</TT><VAR>end</VAR>]<TT>]</TT> <DD> specifies which of the matched filenames should be included in the returned list. The syntax is the same as for array subscripts. <VAR>beg</VAR> and the optional <VAR>end</VAR> may be mathematical expressions. As in parameter subscripting they may be negative to make them count from the last match backward. E.g.: `<TT>*(-OL[1,3])</TT>' gives a list of the names of the three largest files. </DL> <P> More than one of these lists can be combined, separated by commas. The whole list matches if at least one of the sublists matches (they are `or'ed, the qualifiers in the sublists are `and'ed). Some qualifiers, however, affect all matches generated, independent of the sublist in which they are given. These are the qualifiers `<TT>M</TT>', `<TT>T</TT>', `<TT>N</TT>', `<TT>D</TT>', `<TT>n</TT>', `<TT>o</TT>', `<TT>O</TT>' and the subscripts given in brackets (`<TT>[...]</TT>'). <P> If a `<TT>:</TT>' appears in a qualifier list, the remainder of the expression in parenthesis is interpreted as a modifier (see section <A HREF="zsh_13.html#SEC46">Modifiers</A> in section <A HREF="zsh_13.html#SEC42">History Expansion</A>). Note that each modifier must be introduced by a separate `<TT>:</TT>'. Note also that the result after modification does not have to be an existing file. The name of any existing file can be followed by a modifier of the form `<TT>(:..)</TT>' even if no actual filename generation is performed. Thus: <P> <PRE> ls *(-/) </PRE> <P> lists all directories and symbolic links that point to directories, and <P> <PRE> ls *(%W) </PRE> <P> lists all world-writable device files in the current directory, and <P> <PRE> ls *(W,X) </PRE> <P> lists all files in the current directory that are world-writable or world-executable, and <P> <PRE> echo /tmp/foo*(u0^@:t) </PRE> <P> outputs the basename of all root-owned files beginning with the string `<TT>foo</TT>' in <TT>/tmp</TT>, ignoring symlinks, and <P> <PRE> ls *.*~(lex|parse).[ch](^D^l1) </PRE> <P> lists all files having a link count of one whose names contain a dot (but not those starting with a dot, since <TT>GLOB_DOTS</TT> is explicitly switched off) except for <TT>lex.c</TT>, <TT>lex.h</TT>, <TT>parse.c</TT> and <TT>parse.h</TT>. <P><HR><P> Go to the <A HREF="zsh_1.html">first</A>, <A HREF="zsh_12.html">previous</A>, <A HREF="zsh_14.html">next</A>, <A HREF="zsh_29.html">last</A> section, <A HREF="zsh_toc.html">table of contents</A>. </BODY> </HTML>