<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html401/loose.dtd"> <html> <!-- Created on December 20, 2010 by texi2html 1.82 texi2html was written by: Lionel Cons <Lionel.Cons@cern.ch> (original author) Karl Berry <karl@freefriends.org> Olaf Bachmann <obachman@mathematik.uni-kl.de> and many others. Maintained by: Many creative people. Send bugs and suggestions to <texi2html-bug@nongnu.org> --> <head> <title>zsh: 6. Shell Grammar</title> <meta name="description" content="zsh: 6. Shell Grammar"> <meta name="keywords" content="zsh: 6. Shell Grammar"> <meta name="resource-type" content="document"> <meta name="distribution" content="global"> <meta name="Generator" content="texi2html 1.82"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <style type="text/css"> <!-- a.summary-letter {text-decoration: none} blockquote.smallquotation {font-size: smaller} pre.display {font-family: serif} pre.format {font-family: serif} pre.menu-comment {font-family: serif} pre.menu-preformatted {font-family: serif} pre.smalldisplay {font-family: serif; font-size: smaller} pre.smallexample {font-size: smaller} pre.smallformat {font-family: serif; font-size: smaller} pre.smalllisp {font-size: smaller} span.roman {font-family:serif; font-weight:normal;} span.sansserif {font-family:sans-serif; font-weight:normal;} ul.toc {list-style: none} --> </style> </head> <body lang="en" bgcolor="#FFFFFF" text="#000000" link="#0000FF" vlink="#800080" alink="#FF0000"> <a name="Shell-Grammar"></a> <table cellpadding="1" cellspacing="1" border="0"> <tr><td valign="middle" align="left">[<a href="Files.html#Files-2" title="Previous section in reading order"> < </a>]</td> <td valign="middle" align="left">[<a href="#Simple-Commands-_0026-Pipelines" title="Next section in reading order"> > </a>]</td> <td valign="middle" align="left"> </td> <td valign="middle" align="left">[<a href="Files.html#Files" title="Beginning of this chapter or previous chapter"> << </a>]</td> <td valign="middle" align="left">[<a href="zsh.html#Top" title="Up section"> Up </a>]</td> <td valign="middle" align="left">[<a href="Redirection.html#Redirection" title="Next chapter"> >> </a>]</td> <td valign="middle" align="left"> </td> <td valign="middle" align="left"> </td> <td valign="middle" align="left"> </td> <td valign="middle" align="left"> </td> <td valign="middle" align="left">[<a href="zsh.html#Top" title="Cover (top) of document">Top</a>]</td> <td valign="middle" align="left">[<a href="zsh_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td> <td valign="middle" align="left">[<a href="Concept-Index.html#Concept-Index" title="Index">Index</a>]</td> <td valign="middle" align="left">[<a href="zsh_abt.html#SEC_About" title="About (help)"> ? </a>]</td> </tr></table> <a name="Shell-Grammar-1"></a> <h1 class="chapter">6. Shell Grammar</h1> <a name="index-shell-grammar"></a> <a name="index-grammar_002c-shell"></a> <table class="menu" border="0" cellspacing="0"> <tr><td align="left" valign="top"><a href="#Simple-Commands-_0026-Pipelines">6.1 Simple Commands & Pipelines</a></td><td> </td><td align="left" valign="top"> </td></tr> <tr><td align="left" valign="top"><a href="#Precommand-Modifiers">6.2 Precommand Modifiers</a></td><td> </td><td align="left" valign="top"> </td></tr> <tr><td align="left" valign="top"><a href="#Complex-Commands">6.3 Complex Commands</a></td><td> </td><td align="left" valign="top"> </td></tr> <tr><td align="left" valign="top"><a href="#Alternate-Forms-For-Complex-Commands">6.4 Alternate Forms For Complex Commands</a></td><td> </td><td align="left" valign="top"> </td></tr> <tr><td align="left" valign="top"><a href="#Reserved-Words">6.5 Reserved Words</a></td><td> </td><td align="left" valign="top"> </td></tr> <tr><td align="left" valign="top"><a href="#Comments">6.6 Comments</a></td><td> </td><td align="left" valign="top"> </td></tr> <tr><td align="left" valign="top"><a href="#Aliasing">6.7 Aliasing</a></td><td> </td><td align="left" valign="top"> </td></tr> <tr><td align="left" valign="top"><a href="#Quoting">6.8 Quoting</a></td><td> </td><td align="left" valign="top"> </td></tr> </table> <hr size="6"> <a name="Simple-Commands-_0026-Pipelines"></a> <table cellpadding="1" cellspacing="1" border="0"> <tr><td valign="middle" align="left">[<a href="#Shell-Grammar" title="Previous section in reading order"> < </a>]</td> <td valign="middle" align="left">[<a href="#Precommand-Modifiers" title="Next section in reading order"> > </a>]</td> <td valign="middle" align="left"> </td> <td valign="middle" align="left">[<a href="#Shell-Grammar" title="Beginning of this chapter or previous chapter"> << </a>]</td> <td valign="middle" align="left">[<a href="#Shell-Grammar" title="Up section"> Up </a>]</td> <td valign="middle" align="left">[<a href="Redirection.html#Redirection" title="Next chapter"> >> </a>]</td> <td valign="middle" align="left"> </td> <td valign="middle" align="left"> </td> <td valign="middle" align="left"> </td> <td valign="middle" align="left"> </td> <td valign="middle" align="left">[<a href="zsh.html#Top" title="Cover (top) of document">Top</a>]</td> <td valign="middle" align="left">[<a href="zsh_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td> <td valign="middle" align="left">[<a href="Concept-Index.html#Concept-Index" title="Index">Index</a>]</td> <td valign="middle" align="left">[<a href="zsh_abt.html#SEC_About" title="About (help)"> ? </a>]</td> </tr></table> <a name="Simple-Commands-_0026-Pipelines-1"></a> <h2 class="section">6.1 Simple Commands & Pipelines</h2> <a name="index-simple-commands"></a> <a name="index-commands_002c-simple"></a> <p>A <em>simple command</em> is a sequence of optional parameter assignments followed by blank-separated words, with optional redirections interspersed. The first word is the command to be executed, and the remaining words, if any, are arguments to the command. If a command name is given, the parameter assignments modify the environment of the command when it is executed. The value of a simple command is its exit status, or 128 plus the signal number if terminated by a signal. For example, </p> <table><tr><td> </td><td><pre class="example">echo foo </pre></td></tr></table> <p>is a simple command with arguments. </p> <a name="index-pipeline"></a> <p>A <em>pipeline</em> is either a simple command, or a sequence of two or more simple commands where each command is separated from the next by ‘<tt>|</tt>’ or ‘<tt>|&</tt>’. Where commands are separated by ‘<tt>|</tt>’, the standard output of the first command is connected to the standard input of the next. ‘<tt>|&</tt>’ is shorthand for ‘<tt>2>&1 |</tt>’, which connects both the standard output and the standard error of the command to the standard input of the next. The value of a pipeline is the value of the last command, unless the pipeline is preceded by ‘<tt>!</tt>’ in which case the value is the logical inverse of the value of the last command. For example, </p> <table><tr><td> </td><td><pre class="example">echo foo | sed 's/foo/bar/' </pre></td></tr></table> <p>is a pipeline, where the output (‘<tt>foo</tt>’ plus a newline) of the first command will be passed to the input of the second. </p> <a name="index-coproc"></a> <a name="index-coprocess"></a> <p>If a pipeline is preceded by ‘<tt>coproc</tt>’, it is executed as a coprocess; a two-way pipe is established between it and the parent shell. The shell can read from or write to the coprocess by means of the ‘<tt>>&p</tt>’ and ‘<tt><&p</tt>’ redirection operators or with ‘<tt>print -p</tt>’ and ‘<tt>read -p</tt>’. A pipeline cannot be preceded by both ‘<tt>coproc</tt>’ and ‘<tt>!</tt>’. If job control is active, the coprocess can be treated in other than input and output as an ordinary background job. </p> <a name="index-sublist"></a> <p>A <em>sublist</em> is either a single pipeline, or a sequence of two or more pipelines separated by ‘<tt>&&</tt>’ or ‘<tt>||</tt>’. If two pipelines are separated by ‘<tt>&&</tt>’, the second pipeline is executed only if the first succeeds (returns a zero status). If two pipelines are separated by ‘<tt>||</tt>’, the second is executed only if the first fails (returns a nonzero status). Both operators have equal precedence and are left associative. The value of the sublist is the value of the last pipeline executed. For example, </p> <table><tr><td> </td><td><pre class="example">dmesg | grep panic && print yes </pre></td></tr></table> <p>is a sublist consisting of two pipelines, the second just a simple command which will be executed if and only if the <tt>grep</tt> command returns a zero status. If it does not, the value of the sublist is that return status, else it is the status returned by the <tt>print</tt> (almost certainly zero). </p> <a name="index-list"></a> <p>A <em>list</em> is a sequence of zero or more sublists, in which each sublist is terminated by ‘<tt>;</tt>’, ‘<tt>&</tt>’, ‘<tt>&|</tt>’, ‘<tt>&!</tt>’, or a newline. This terminator may optionally be omitted from the last sublist in the list when the list appears as a complex command inside ‘<tt>(</tt>...<tt>)</tt>’ or ‘<tt>{</tt>...<tt>}</tt>’. When a sublist is terminated by ‘<tt>;</tt>’ or newline, the shell waits for it to finish before executing the next sublist. If a sublist is terminated by a ‘<tt>&</tt>’, ‘<tt>&|</tt>’, or ‘<tt>&!</tt>’, the shell executes the last pipeline in it in the background, and does not wait for it to finish (note the difference from other shells which execute the whole sublist in the background). A backgrounded pipeline returns a status of zero. </p> <p>More generally, a list can be seen as a set of any shell commands whatsoever, including the complex commands below; this is implied wherever the word ‘list’ appears in later descriptions. For example, the commands in a shell function form a special sort of list. </p><hr size="6"> <a name="Precommand-Modifiers"></a> <table cellpadding="1" cellspacing="1" border="0"> <tr><td valign="middle" align="left">[<a href="#Simple-Commands-_0026-Pipelines" title="Previous section in reading order"> < </a>]</td> <td valign="middle" align="left">[<a href="#Complex-Commands" title="Next section in reading order"> > </a>]</td> <td valign="middle" align="left"> </td> <td valign="middle" align="left">[<a href="#Shell-Grammar" title="Beginning of this chapter or previous chapter"> << </a>]</td> <td valign="middle" align="left">[<a href="#Shell-Grammar" title="Up section"> Up </a>]</td> <td valign="middle" align="left">[<a href="Redirection.html#Redirection" title="Next chapter"> >> </a>]</td> <td valign="middle" align="left"> </td> <td valign="middle" align="left"> </td> <td valign="middle" align="left"> </td> <td valign="middle" align="left"> </td> <td valign="middle" align="left">[<a href="zsh.html#Top" title="Cover (top) of document">Top</a>]</td> <td valign="middle" align="left">[<a href="zsh_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td> <td valign="middle" align="left">[<a href="Concept-Index.html#Concept-Index" title="Index">Index</a>]</td> <td valign="middle" align="left">[<a href="zsh_abt.html#SEC_About" title="About (help)"> ? </a>]</td> </tr></table> <a name="Precommand-Modifiers-1"></a> <h2 class="section">6.2 Precommand Modifiers</h2> <a name="index-precommand-modifiers"></a> <a name="index-modifiers_002c-precommand"></a> <p>A simple command may be preceded by a <em>precommand modifier</em>, which will alter how the command is interpreted. These modifiers are shell builtin commands with the exception of <tt>nocorrect</tt> which is a reserved word. </p> <dl compact="compact"> <dd><a name="index-_002d"></a> </dd> <dt> <tt>-</tt></dt> <dd><p>The command is executed with a ‘<tt>-</tt>’ prepended to its <tt>argv[0]</tt> string. </p> <a name="index-builtin"></a> </dd> <dt> <tt>builtin</tt></dt> <dd><p>The command word is taken to be the name of a builtin command, rather than a shell function or external command. </p> <a name="index-command"></a> </dd> <dt> <tt>command</tt> [ <tt>-pvV</tt> ]</dt> <dd><p>The command word is taken to be the name of an external command, rather than a shell function or builtin. If the <tt>POSIX_BUILTINS</tt> option is set, builtins will also be executed but certain special properties of them are suppressed. The <tt>-p</tt> flag causes a default path to be searched instead of that in <tt>$path</tt>. With the <tt>-v</tt> flag, <tt>command</tt> is similar to <tt>whence</tt> and with <tt>-V</tt>, it is equivalent to <tt>whence -v</tt>. </p> <a name="index-exec"></a> </dd> <dt> <tt>exec</tt> [ <tt>-cl</tt> ] [ <tt>-a</tt> <var>argv0</var> ]</dt> <dd><p>The following command together with any arguments is run in place of the current process, rather than as a sub-process. The shell does not fork and is replaced. The shell does not invoke <tt>TRAPEXIT</tt>, nor does it source <tt>zlogout</tt> files. The options are provided for compatibility with other shells. </p> <p>The <tt>-c</tt> option clears the environment. </p> <p>The <tt>-l</tt> option is equivalent to the <tt>-</tt> precommand modifier, to treat the replacement command as a login shell; the command is executed with a <tt>-</tt> prepended to its <tt>argv[0]</tt> string. This flag has no effect if used together with the <tt>-a</tt> option. </p> <p>The <tt>-a</tt> option is used to specify explicitly the <tt>argv[0]</tt> string (the name of the command as seen by the process itself) to be used by the replacement command and is directly equivalent to setting a value for the <tt>ARGV0</tt> environment variable. </p> <a name="index-nocorrect"></a> </dd> <dt> <tt>nocorrect</tt></dt> <dd><p>Spelling correction is not done on any of the words. This must appear before any other precommand modifier, as it is interpreted immediately, before any parsing is done. It has no effect in non-interactive shells. </p> <a name="index-noglob"></a> </dd> <dt> <tt>noglob</tt></dt> <dd><p>Filename generation (globbing) is not performed on any of the words. </p> </dd> </dl> <hr size="6"> <a name="Complex-Commands"></a> <table cellpadding="1" cellspacing="1" border="0"> <tr><td valign="middle" align="left">[<a href="#Precommand-Modifiers" title="Previous section in reading order"> < </a>]</td> <td valign="middle" align="left">[<a href="#Alternate-Forms-For-Complex-Commands" title="Next section in reading order"> > </a>]</td> <td valign="middle" align="left"> </td> <td valign="middle" align="left">[<a href="#Shell-Grammar" title="Beginning of this chapter or previous chapter"> << </a>]</td> <td valign="middle" align="left">[<a href="#Shell-Grammar" title="Up section"> Up </a>]</td> <td valign="middle" align="left">[<a href="Redirection.html#Redirection" title="Next chapter"> >> </a>]</td> <td valign="middle" align="left"> </td> <td valign="middle" align="left"> </td> <td valign="middle" align="left"> </td> <td valign="middle" align="left"> </td> <td valign="middle" align="left">[<a href="zsh.html#Top" title="Cover (top) of document">Top</a>]</td> <td valign="middle" align="left">[<a href="zsh_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td> <td valign="middle" align="left">[<a href="Concept-Index.html#Concept-Index" title="Index">Index</a>]</td> <td valign="middle" align="left">[<a href="zsh_abt.html#SEC_About" title="About (help)"> ? </a>]</td> </tr></table> <a name="Complex-Commands-1"></a> <h2 class="section">6.3 Complex Commands</h2> <a name="index-complex-commands"></a> <a name="index-commands_002c-complex"></a> <p>A <em>complex command</em> in zsh is one of the following: </p> <dl compact="compact"> <dd><a name="index-if"></a> <a name="index-if-construct"></a> </dd> <dt> <tt>if</tt> <var>list</var> <tt>then</tt> <var>list</var> [ <tt>elif</tt> <var>list</var> <tt>then</tt> <var>list</var> ] ... [ <tt>else</tt> <var>list</var> ] <tt>fi</tt></dt> <dd><p>The <tt>if</tt> <var>list</var> is executed, and if it returns a zero exit status, the <tt>then</tt> <var>list</var> is executed. Otherwise, the <tt>elif</tt> <var>list</var> is executed and if its status is zero, the <tt>then</tt> <var>list</var> is executed. If each <tt>elif</tt> <var>list</var> returns nonzero status, the <tt>else</tt> <var>list</var> is executed. </p> <a name="index-for"></a> <a name="index-for-loops"></a> <a name="index-loops_002c-for"></a> </dd> <dt> <tt>for</tt> <var>name</var> ... [ <tt>in</tt> <var>word</var> ... ] <var>term</var> <tt>do</tt> <var>list</var> <tt>done</tt></dt> <dd><p>where <var>term</var> is at least one newline or <tt>;</tt>. Expand the list of <var>word</var>s, and set the parameter <var>name</var> to each of them in turn, executing <var>list</var> each time. If the <tt>in</tt> <var>word</var> is omitted, use the positional parameters instead of the <var>word</var>s. </p> <p>More than one parameter <var>name</var> can appear before the list of <var>word</var>s. If <var>N</var> <var>name</var>s are given, then on each execution of the loop the next <tt>N</tt> <var>word</var>s are assigned to the corresponding parameters. If there are more <var>name</var>s than remaining <var>word</var>s, the remaining parameters are each set to the empty string. Execution of the loop ends when there is no remaining <var>word</var> to assign to the first <var>name</var>. It is only possible for <tt>in</tt> to appear as the first <var>name</var> in the list, else it will be treated as marking the end of the list. </p> </dd> <dt> <tt>for ((</tt> [<var>expr1</var>] <tt>;</tt> [<var>expr2</var>] <tt>;</tt> [<var>expr3</var>] <tt>)) do</tt> <var>list</var> <tt>done</tt></dt> <dd><p>The arithmetic expression <var>expr1</var> is evaluated first (see <a href="Arithmetic-Evaluation.html#Arithmetic-Evaluation">Arithmetic Evaluation</a>). The arithmetic expression <var>expr2</var> is repeatedly evaluated until it evaluates to zero and when non-zero, <var>list</var> is executed and the arithmetic expression <var>expr3</var> evaluated. If any expression is omitted, then it behaves as if it evaluated to 1. </p> <a name="index-while"></a> <a name="index-while-loops"></a> <a name="index-loops_002c-while"></a> </dd> <dt> <tt>while</tt> <var>list</var> <tt>do</tt> <var>list</var> <tt>done</tt></dt> <dd><p>Execute the <tt>do</tt> <var>list</var> as long as the <tt>while</tt> <var>list</var> returns a zero exit status. </p> <a name="index-until"></a> <a name="index-until-loops"></a> <a name="index-loops_002c-until"></a> </dd> <dt> <tt>until</tt> <var>list</var> <tt>do</tt> <var>list</var> <tt>done</tt></dt> <dd><p>Execute the <tt>do</tt> <var>list</var> as long as <tt>until</tt> <var>list</var> returns a nonzero exit status. </p> <a name="index-repeat"></a> <a name="index-repeat-loops"></a> <a name="index-loops_002c-repeat"></a> </dd> <dt> <tt>repeat</tt> <var>word</var> <tt>do</tt> <var>list</var> <tt>done</tt></dt> <dd><p><var>word</var> is expanded and treated as an arithmetic expression, which must evaluate to a number <var>n</var>. <var>list</var> is then executed <var>n</var> times. </p> <p>The <tt>repeat</tt> syntax is disabled by default when the shell starts in a mode emulating another shell. It can be enabled with the command ‘<tt>enable -r repeat</tt>’ </p> <a name="index-case"></a> <a name="index-case-selection"></a> <a name="index-selection_002c-case"></a> </dd> <dt> <tt>case</tt> <var>word</var> <tt>in</tt> [ [<tt>(</tt>] <var>pattern</var> [ <tt>|</tt> <var>pattern</var> ] ... <tt>)</tt> <var>list</var> (<tt>;;</tt>|<tt>;&</tt>|<tt>;|</tt>) ] ... <tt>esac</tt></dt> <dd><p>Execute the <var>list</var> associated with the first <var>pattern</var> that matches <var>word</var>, if any. The form of the patterns is the same as that used for filename generation. See <a href="Expansion.html#Filename-Generation">Filename Generation</a>. </p> <p>If the <var>list</var> that is executed is terminated with <tt>;&</tt> rather than <tt>;;</tt>, the following list is also executed. The rule for the terminator of the following list <tt>;;</tt>, <tt>;&</tt> or <tt>;|</tt> is applied unless the <tt>esac</tt> is reached. </p> <p>If the <var>list</var> that is executed is terminated with <tt>;|</tt> the shell continues to scan the <var>pattern</var>s looking for the next match, executing the corresponding <var>list</var>, and applying the rule for the corresponding terminator <tt>;;</tt>, <tt>;&</tt> or <tt>;|</tt>. Note that <var>word</var> is not re-expanded; all applicable <var>pattern</var>s are tested with the same <var>word</var>. </p> <a name="index-select"></a> <a name="index-user-selection"></a> <a name="index-selection_002c-user"></a> </dd> <dt> <tt>select</tt> <var>name</var> [ <tt>in</tt> <var>word</var> ... <var>term</var> ] <tt>do</tt> <var>list</var> <tt>done</tt></dt> <dd><p>where <var>term</var> is one or more newline or <tt>;</tt> to terminate the <var>word</var>s. <a name="index-REPLY_002c-use-of"></a> Print the set of <var>word</var>s, each preceded by a number. If the <tt>in</tt> <var>word</var> is omitted, use the positional parameters. The <tt>PROMPT3</tt> prompt is printed and a line is read from the line editor if the shell is interactive and that is active, or else standard input. If this line consists of the number of one of the listed <var>word</var>s, then the parameter <var>name</var> is set to the <var>word</var> corresponding to this number. If this line is empty, the selection list is printed again. Otherwise, the value of the parameter <var>name</var> is set to null. The contents of the line read from standard input is saved in the parameter <tt>REPLY</tt>. <var>list</var> is executed for each selection until a break or end-of-file is encountered. </p> <a name="index-subshell"></a> </dd> <dt> <tt>(</tt> <var>list</var> <tt>)</tt></dt> <dd><p>Execute <var>list</var> in a subshell. Traps set by the <tt>trap</tt> builtin are reset to their default values while executing <var>list</var>. </p> </dd> <dt> <tt>{</tt> <var>list</var> <tt>}</tt></dt> <dd><p>Execute <var>list</var>. </p> <a name="index-always"></a> <a name="index-always-blocks"></a> <a name="index-try-blocks"></a> </dd> <dt> <tt>{</tt> <var>try-list</var> <tt>} always {</tt> <var>always-list</var> <tt>}</tt></dt> <dd><p>First execute <var>try-list</var>. Regardless of errors, or <tt>break</tt>, <tt>continue</tt>, or <tt>return</tt> commands encountered within <var>try-list</var>, execute <var>always-list</var>. Execution then continues from the result of the execution of <var>try-list</var>; in other words, any error, or <tt>break</tt>, <tt>continue</tt>, or <tt>return</tt> command is treated in the normal way, as if <var>always-list</var> were not present. The two chunks of code are referred to as the ‘try block’ and the ‘always block’. </p> <p>Optional newlines or semicolons may appear after the <tt>always</tt>; note, however, that they may <em>not</em> appear between the preceding closing brace and the <tt>always</tt>. </p> <p>An ‘error’ in this context is a condition such as a syntax error which causes the shell to abort execution of the current function, script, or list. Syntax errors encountered while the shell is parsing the code do not cause the <var>always-list</var> to be executed. For example, an erroneously constructed <tt>if</tt> block in <tt>try-list</tt> would cause the shell to abort during parsing, so that <tt>always-list</tt> would not be executed, while an erroneous substitution such as <tt>${*foo*}</tt> would cause a run-time error, after which <tt>always-list</tt> would be executed. </p> <p>An error condition can be tested and reset with the special integer variable <tt>TRY_BLOCK_ERROR</tt>. Outside an <tt>always-list</tt> the value is irrelevant, but it is initialised to <tt>-1</tt>. Inside <tt>always-list</tt>, the value is 1 if an error occurred in the <tt>try-list</tt>, else 0. If <tt>TRY_BLOCK_ERROR</tt> is set to 0 during the <tt>always-list</tt>, the error condition caused by the <tt>try-list</tt> is reset, and shell execution continues normally after the end of <tt>always-list</tt>. Altering the value during the <tt>try-list</tt> is not useful (unless this forms part of an enclosing <tt>always</tt> block). </p> <p>Regardless of <tt>TRY_BLOCK_ERROR</tt>, after the end of <tt>always-list</tt> the normal shell status <tt>$?</tt> is the value returned from <tt>always-list</tt>. This will be non-zero if there was an error, even if <tt>TRY_BLOCK_ERROR</tt> was set to zero. </p> <p>The following executes the given code, ignoring any errors it causes. This is an alternative to the usual convention of protecting code by executing it in a subshell. </p> <table><tr><td> </td><td><pre class="example">{ # code which may cause an error } always { # This code is executed regardless of the error. (( TRY_BLOCK_ERROR = 0 )) } # The error condition has been reset. </pre></td></tr></table> <p>An <tt>exit</tt> command (or a <tt>return</tt> command executed at the outermost function level of a script) encountered in <tt>try-list</tt> does <em>not</em> cause the execution of <var>always-list</var>. Instead, the shell exits immediately after any <tt>EXIT</tt> trap has been executed. </p> <a name="index-function"></a> </dd> <dt> <tt>function</tt> <var>word</var> ... [ <tt>()</tt> ] [ <var>term</var> ] <tt>{</tt> <var>list</var> <tt>}</tt></dt> <dt> <var>word</var> ... <tt>()</tt> [ <var>term</var> ] <tt>{</tt> <var>list</var> <tt>}</tt></dt> <dt> <var>word</var> ... <tt>()</tt> [ <var>term</var> ] <var>command</var></dt> <dd><p>where <var>term</var> is one or more newline or <tt>;</tt>. Define a function which is referenced by any one of <var>word</var>. Normally, only one <var>word</var> is provided; multiple <var>word</var>s are usually only useful for setting traps. The body of the function is the <var>list</var> between the <tt>{</tt> and <tt>}</tt>. See <a href="Functions.html#Functions">Functions</a>. </p> <p>If the option <tt>SH_GLOB</tt> is set for compatibility with other shells, then whitespace may appear between between the left and right parentheses when there is a single <var>word</var>; otherwise, the parentheses will be treated as forming a globbing pattern in that case. </p> <a name="index-timing"></a> <a name="index-time"></a> </dd> <dt> <tt>time</tt> [ <var>pipeline</var> ]</dt> <dd><p>The <var>pipeline</var> is executed, and timing statistics are reported on the standard error in the form specified by the <tt>TIMEFMT</tt> parameter. If <var>pipeline</var> is omitted, print statistics about the shell process and its children. </p> <a name="index-conditional-expression"></a> <a name="index-_005b_005b"></a> </dd> <dt> <tt>[[</tt> <var>exp</var> <tt>]]</tt></dt> <dd><p>Evaluates the conditional expression <var>exp</var> and return a zero exit status if it is true. See <a href="Conditional-Expressions.html#Conditional-Expressions">Conditional Expressions</a> for a description of <var>exp</var>. </p> </dd> </dl> <hr size="6"> <a name="Alternate-Forms-For-Complex-Commands"></a> <table cellpadding="1" cellspacing="1" border="0"> <tr><td valign="middle" align="left">[<a href="#Complex-Commands" title="Previous section in reading order"> < </a>]</td> <td valign="middle" align="left">[<a href="#Reserved-Words" title="Next section in reading order"> > </a>]</td> <td valign="middle" align="left"> </td> <td valign="middle" align="left">[<a href="#Shell-Grammar" title="Beginning of this chapter or previous chapter"> << </a>]</td> <td valign="middle" align="left">[<a href="#Shell-Grammar" title="Up section"> Up </a>]</td> <td valign="middle" align="left">[<a href="Redirection.html#Redirection" title="Next chapter"> >> </a>]</td> <td valign="middle" align="left"> </td> <td valign="middle" align="left"> </td> <td valign="middle" align="left"> </td> <td valign="middle" align="left"> </td> <td valign="middle" align="left">[<a href="zsh.html#Top" title="Cover (top) of document">Top</a>]</td> <td valign="middle" align="left">[<a href="zsh_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td> <td valign="middle" align="left">[<a href="Concept-Index.html#Concept-Index" title="Index">Index</a>]</td> <td valign="middle" align="left">[<a href="zsh_abt.html#SEC_About" title="About (help)"> ? </a>]</td> </tr></table> <a name="Alternate-Forms-For-Complex-Commands-1"></a> <h2 class="section">6.4 Alternate Forms For Complex Commands</h2> <a name="index-alternate-forms-for-complex-commands"></a> <a name="index-commands_002c-alternate-forms-for-complex"></a> <p>Many of zsh’s complex commands have alternate forms. These are non-standard and are likely not to be obvious even to seasoned shell programmers; they should not be used anywhere that portability of shell code is a concern. </p> <p>The short versions below only work if <var>sublist</var> is of the form ‘<tt>{</tt> <var>list</var> <tt>}</tt>’ or if the <tt>SHORT_LOOPS</tt> option is set. For the <tt>if</tt>, <tt>while</tt> and <tt>until</tt> commands, in both these cases the test part of the loop must also be suitably delimited, such as by ‘<tt>[[ ... ]]</tt>’ or ‘<tt>(( ... ))</tt>’, else the end of the test will not be recognized. For the <tt>for</tt>, <tt>repeat</tt>, <tt>case</tt> and <tt>select</tt> commands no such special form for the arguments is necessary, but the other condition (the special form of <var>sublist</var> or use of the <tt>SHORT_LOOPS</tt> option) still applies. </p> <dl compact="compact"> <dt> <tt>if</tt> <var>list</var> <tt>{</tt> <var>list</var> <tt>}</tt> [ <tt>elif</tt> <var>list</var> <tt>{</tt> <var>list</var> <tt>}</tt> ] ... [ <tt>else {</tt> <var>list</var> <tt>}</tt> ]</dt> <dd><p>An alternate form of <tt>if</tt>. The rules mean that </p> <table><tr><td> </td><td><pre class="example">if [[ -o ignorebraces ]] { print yes } </pre></td></tr></table> <p>works, but </p> <table><tr><td> </td><td><pre class="example">if true { # Does not work! print yes } </pre></td></tr></table> <p>does <em>not</em>, since the test is not suitably delimited. </p> </dd> <dt> <tt>if</tt> <var>list</var> <var>sublist</var></dt> <dd><p>A short form of the alternate ‘if’. The same limitations on the form of <var>list</var> apply as for the previous form. </p> </dd> <dt> <tt>for</tt> <var>name</var> ... <tt>(</tt> <var>word</var> ... <tt>)</tt> <var>sublist</var></dt> <dd><p>A short form of <tt>for</tt>. </p> </dd> <dt> <tt>for</tt> <var>name</var> ... [ <tt>in</tt> <var>word</var> ... ] <var>term</var> <var>sublist</var></dt> <dd><p>where <var>term</var> is at least one newline or <tt>;</tt>. Another short form of <tt>for</tt>. </p> </dd> <dt> <tt>for ((</tt> [<var>expr1</var>] <tt>;</tt> [<var>expr2</var>] <tt>;</tt> [<var>expr3</var>] <tt>))</tt> <var>sublist</var></dt> <dd><p>A short form of the arithmetic <tt>for</tt> command. </p> <a name="index-foreach"></a> </dd> <dt> <tt>foreach</tt> <var>name</var> ... <tt>(</tt> <var>word</var> ... <tt>)</tt> <var>list</var> <tt>end</tt></dt> <dd><p>Another form of <tt>for</tt>. </p> </dd> <dt> <tt>while</tt> <var>list</var> <tt>{</tt> <var>list</var> <tt>}</tt></dt> <dd><p>An alternative form of <tt>while</tt>. Note the limitations on the form of <var>list</var> mentioned above. </p> </dd> <dt> <tt>until</tt> <var>list</var> <tt>{</tt> <var>list</var> <tt>}</tt></dt> <dd><p>An alternative form of <tt>until</tt>. Note the limitations on the form of <var>list</var> mentioned above. </p> </dd> <dt> <tt>repeat</tt> <var>word</var> <var>sublist</var></dt> <dd><p>This is a short form of <tt>repeat</tt>. </p> </dd> <dt> <tt>case</tt> <var>word</var> <tt>{</tt> [ [<tt>(</tt>] <var>pattern</var> [ <tt>|</tt> <var>pattern</var> ] ... <tt>)</tt> <var>list</var> (<tt>;;</tt>|<tt>;&</tt>|<tt>;|</tt>) ] ... <tt>}</tt></dt> <dd><p>An alternative form of <tt>case</tt>. </p> </dd> <dt> <tt>select</tt> <var>name</var> [ <tt>in</tt> <var>word</var> <var>term</var> ] <var>sublist</var></dt> <dd><p>where <var>term</var> is at least one newline or <tt>;</tt>. A short form of <tt>select</tt>. </p> </dd> </dl> <hr size="6"> <a name="Reserved-Words"></a> <table cellpadding="1" cellspacing="1" border="0"> <tr><td valign="middle" align="left">[<a href="#Alternate-Forms-For-Complex-Commands" title="Previous section in reading order"> < </a>]</td> <td valign="middle" align="left">[<a href="#Comments" title="Next section in reading order"> > </a>]</td> <td valign="middle" align="left"> </td> <td valign="middle" align="left">[<a href="#Shell-Grammar" title="Beginning of this chapter or previous chapter"> << </a>]</td> <td valign="middle" align="left">[<a href="#Shell-Grammar" title="Up section"> Up </a>]</td> <td valign="middle" align="left">[<a href="Redirection.html#Redirection" title="Next chapter"> >> </a>]</td> <td valign="middle" align="left"> </td> <td valign="middle" align="left"> </td> <td valign="middle" align="left"> </td> <td valign="middle" align="left"> </td> <td valign="middle" align="left">[<a href="zsh.html#Top" title="Cover (top) of document">Top</a>]</td> <td valign="middle" align="left">[<a href="zsh_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td> <td valign="middle" align="left">[<a href="Concept-Index.html#Concept-Index" title="Index">Index</a>]</td> <td valign="middle" align="left">[<a href="zsh_abt.html#SEC_About" title="About (help)"> ? </a>]</td> </tr></table> <a name="Reserved-Words-1"></a> <h2 class="section">6.5 Reserved Words</h2> <a name="index-reserved-words"></a> <a name="index-disable_002c-use-of"></a> <p>The following words are recognized as reserved words when used as the first word of a command unless quoted or disabled using <tt>disable -r</tt>: </p> <p><tt>do done esac then elif else fi for case if while function repeat time until select coproc nocorrect foreach end ! [[ { }</tt> </p> <p>Additionally, ‘<tt>}</tt>’ is recognized in any position if the <tt>IGNORE_BRACES</tt> option is not set. </p><hr size="6"> <a name="Comments"></a> <table cellpadding="1" cellspacing="1" border="0"> <tr><td valign="middle" align="left">[<a href="#Reserved-Words" title="Previous section in reading order"> < </a>]</td> <td valign="middle" align="left">[<a href="#Aliasing" title="Next section in reading order"> > </a>]</td> <td valign="middle" align="left"> </td> <td valign="middle" align="left">[<a href="#Shell-Grammar" title="Beginning of this chapter or previous chapter"> << </a>]</td> <td valign="middle" align="left">[<a href="#Shell-Grammar" title="Up section"> Up </a>]</td> <td valign="middle" align="left">[<a href="Redirection.html#Redirection" title="Next chapter"> >> </a>]</td> <td valign="middle" align="left"> </td> <td valign="middle" align="left"> </td> <td valign="middle" align="left"> </td> <td valign="middle" align="left"> </td> <td valign="middle" align="left">[<a href="zsh.html#Top" title="Cover (top) of document">Top</a>]</td> <td valign="middle" align="left">[<a href="zsh_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td> <td valign="middle" align="left">[<a href="Concept-Index.html#Concept-Index" title="Index">Index</a>]</td> <td valign="middle" align="left">[<a href="zsh_abt.html#SEC_About" title="About (help)"> ? </a>]</td> </tr></table> <a name="Comments-1"></a> <h2 class="section">6.6 Comments</h2> <a name="index-comments"></a> <a name="index-INTERACTIVE_005fCOMMENTS_002c-use-of"></a> <a name="index-histchars_002c-use-of"></a> <p>In non-interactive shells, or in interactive shells with the <tt>INTERACTIVE_COMMENTS</tt> option set, a word beginning with the third character of the <tt>histchars</tt> parameter (‘<tt>#</tt>’ by default) causes that word and all the following characters up to a newline to be ignored. </p><hr size="6"> <a name="Aliasing"></a> <table cellpadding="1" cellspacing="1" border="0"> <tr><td valign="middle" align="left">[<a href="#Comments" title="Previous section in reading order"> < </a>]</td> <td valign="middle" align="left">[<a href="#Quoting" title="Next section in reading order"> > </a>]</td> <td valign="middle" align="left"> </td> <td valign="middle" align="left">[<a href="#Shell-Grammar" title="Beginning of this chapter or previous chapter"> << </a>]</td> <td valign="middle" align="left">[<a href="#Shell-Grammar" title="Up section"> Up </a>]</td> <td valign="middle" align="left">[<a href="Redirection.html#Redirection" title="Next chapter"> >> </a>]</td> <td valign="middle" align="left"> </td> <td valign="middle" align="left"> </td> <td valign="middle" align="left"> </td> <td valign="middle" align="left"> </td> <td valign="middle" align="left">[<a href="zsh.html#Top" title="Cover (top) of document">Top</a>]</td> <td valign="middle" align="left">[<a href="zsh_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td> <td valign="middle" align="left">[<a href="Concept-Index.html#Concept-Index" title="Index">Index</a>]</td> <td valign="middle" align="left">[<a href="zsh_abt.html#SEC_About" title="About (help)"> ? </a>]</td> </tr></table> <a name="Aliasing-1"></a> <h2 class="section">6.7 Aliasing</h2> <a name="index-aliasing"></a> <p>Every token in the shell input is checked to see if there is an alias defined for it. If so, it is replaced by the text of the alias if it is in command position (if it could be the first word of a simple command), or if the alias is global. If the text ends with a space, the next word in the shell input is treated as though it were in command position for purposes of alias expansion. <a name="index-alias_002c-use-of"></a> <a name="index-aliases_002c-global"></a> An alias is defined using the <tt>alias</tt> builtin; global aliases may be defined using the <tt>-g</tt> option to that builtin. </p> <p>Alias expansion is done on the shell input before any other expansion except history expansion. Therefore, if an alias is defined for the word <tt>foo</tt>, alias expansion may be avoided by quoting part of the word, e.g. <tt>\foo</tt>. Any form of quoting works, although there is nothing to prevent an alias being defined for the quoted form such as <tt>\foo</tt> as well. For use with completion, which would remove an initial backslash followed by a character that isn’t special, it may be more convenient to quote the word by starting with a single quote, i.e. <tt>’foo</tt>; completion will automatically add the trailing single quote. </p> <p>There is a commonly encountered problem with aliases illustrated by the following code: </p> <table><tr><td> </td><td><pre class="example">alias echobar='echo bar'; echobar </pre></td></tr></table> <p>This prints a message that the command <tt>echobar</tt> could not be found. This happens because aliases are expanded when the code is read in; the entire line is read in one go, so that when <tt>echobar</tt> is executed it is too late to expand the newly defined alias. This is often a problem in shell scripts, functions, and code executed with ‘<tt>source</tt>’ or ‘<tt>.</tt>’. Consequently, use of functions rather than aliases is recommended in non-interactive code. </p> <p>Note also the unhelpful interaction of aliases and function definitions: </p> <table><tr><td> </td><td><pre class="example">alias func='noglob func' func() { echo Do something with $* } </pre></td></tr></table> <p>Because aliases are expanded in function defintions, this causes the following command to be executed: </p> <table><tr><td> </td><td><pre class="example">noglob func() { echo Do something with $* } </pre></td></tr></table> <p>which defines <tt>noglob</tt> as well as <tt>func</tt> as functions with the body given. To avoid this, either quote the name <tt>func</tt> or use the alternative function definition form ‘<tt>function func</tt>’. Ensuring the alias is defined after the function works but is problematic if the code fragment might be re-executed. </p> <hr size="6"> <a name="Quoting"></a> <table cellpadding="1" cellspacing="1" border="0"> <tr><td valign="middle" align="left">[<a href="#Aliasing" title="Previous section in reading order"> < </a>]</td> <td valign="middle" align="left">[<a href="Redirection.html#Redirection" title="Next section in reading order"> > </a>]</td> <td valign="middle" align="left"> </td> <td valign="middle" align="left">[<a href="#Shell-Grammar" title="Beginning of this chapter or previous chapter"> << </a>]</td> <td valign="middle" align="left">[<a href="#Shell-Grammar" title="Up section"> Up </a>]</td> <td valign="middle" align="left">[<a href="Redirection.html#Redirection" title="Next chapter"> >> </a>]</td> <td valign="middle" align="left"> </td> <td valign="middle" align="left"> </td> <td valign="middle" align="left"> </td> <td valign="middle" align="left"> </td> <td valign="middle" align="left">[<a href="zsh.html#Top" title="Cover (top) of document">Top</a>]</td> <td valign="middle" align="left">[<a href="zsh_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td> <td valign="middle" align="left">[<a href="Concept-Index.html#Concept-Index" title="Index">Index</a>]</td> <td valign="middle" align="left">[<a href="zsh_abt.html#SEC_About" title="About (help)"> ? </a>]</td> </tr></table> <a name="Quoting-1"></a> <h2 class="section">6.8 Quoting</h2> <a name="index-quoting"></a> <p>A character may be <var>quoted</var> (that is, made to stand for itself) by preceding it with a ‘<tt>\</tt>’. ‘<tt>\</tt>’ followed by a newline is ignored. </p> <p>A string enclosed between ‘<tt>$’</tt>’ and ‘<tt>’</tt>’ is processed the same way as the string arguments of the <tt>print</tt> builtin, and the resulting string is considered to be entirely quoted. A literal ‘<tt>’</tt>’ character can be included in the string by using the ‘<tt>\’</tt>’ escape. </p> <a name="index-RC_005fQUOTES_002c-use-of"></a> <p>All characters enclosed between a pair of single quotes (<tt>’’</tt>) that is not preceded by a ‘<tt>$</tt>’ are quoted. A single quote cannot appear within single quotes unless the option <tt>RC_QUOTES</tt> is set, in which case a pair of single quotes are turned into a single quote. For example, </p> <table><tr><td> </td><td><pre class="example">print '''' </pre></td></tr></table> <p>outputs nothing apart from a newline if <tt>RC_QUOTES</tt> is not set, but one single quote if it is set. </p> <p>Inside double quotes (<tt>""</tt>), parameter and command substitution occur, and ‘<tt>\</tt>’ quotes the characters ‘<tt>\</tt>’, ‘<tt>‘</tt>’, ‘<tt>"</tt>’, and ‘<tt>$</tt>’. </p><hr size="6"> <table cellpadding="1" cellspacing="1" border="0"> <tr><td valign="middle" align="left">[<a href="#Shell-Grammar" title="Beginning of this chapter or previous chapter"> << </a>]</td> <td valign="middle" align="left">[<a href="Redirection.html#Redirection" title="Next chapter"> >> </a>]</td> <td valign="middle" align="left"> </td> <td valign="middle" align="left"> </td> <td valign="middle" align="left"> </td> <td valign="middle" align="left"> </td> <td valign="middle" align="left"> </td> <td valign="middle" align="left">[<a href="zsh.html#Top" title="Cover (top) of document">Top</a>]</td> <td valign="middle" align="left">[<a href="zsh_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td> <td valign="middle" align="left">[<a href="Concept-Index.html#Concept-Index" title="Index">Index</a>]</td> <td valign="middle" align="left">[<a href="zsh_abt.html#SEC_About" title="About (help)"> ? </a>]</td> </tr></table> <p> <font size="-1"> This document was generated by <em>Peter Stephenson</em> on <em>December 20, 2010</em> using <a href="http://www.nongnu.org/texi2html/"><em>texi2html 1.82</em></a>. </font> <br> </p> </body> </html>