Files
oldlinux-files/Ref-docs/POSIX/susv3/utilities/xcu_chap02.html
2024-02-19 00:21:47 -05:00

2828 lines
121 KiB
HTML

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta name="generator" content="HTML Tidy, see www.w3.org">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link type="text/css" rel="stylesheet" href="style.css"><!-- Generated by The Open Group's rhtm tool v1.2.1 -->
<!-- Copyright (c) 2001 The Open Group, All Rights Reserved -->
<title>Shell Command Language</title>
</head>
<body bgcolor="white">
<script type="text/javascript" language="JavaScript" src="../jscript/codes.js">
</script>
<basefont size="3"> <!--header start-->
<center><font size="2">The Open Group Base Specifications Issue 6<br>
IEEE Std 1003.1-2001<br>
Copyright &copy; 2001 The IEEE and The Open Group, All Rights reserved.</font></center>
<!--header end-->
<hr size="2" noshade>
<h2><a name="tag_02"></a>Shell Command Language</h2>
<p>This chapter contains the definition of the Shell Command Language.</p>
<h3><a name="tag_02_01"></a>Shell Introduction</h3>
<p>The shell is a command language interpreter. This chapter describes the syntax of that command language as it is used by the <a
href="../utilities/sh.html"><i>sh</i></a> utility and the <a href="../functions/system.html"><i>system</i>()</a> and <a href=
"../functions/popen.html"><i>popen</i>()</a> functions defined in the System Interfaces volume of
IEEE&nbsp;Std&nbsp;1003.1-2001.</p>
<p>The shell operates according to the following general overview of operations. The specific details are included in the cited
sections of this chapter.</p>
<ol>
<li>
<p>The shell reads its input from a file (see <a href="../utilities/sh.html"><i>sh</i></a>), from the <b>-c</b> option or from the
<a href="../functions/system.html"><i>system</i>()</a> and <a href="../functions/popen.html"><i>popen</i>()</a> functions defined
in the System Interfaces volume of IEEE&nbsp;Std&nbsp;1003.1-2001. If the first line of a file of shell commands starts with the
characters <tt>"#!"</tt> , the results are unspecified.</p>
</li>
<li>
<p>The shell breaks the input into tokens: words and operators; see <a href="#tag_02_03">Token Recognition</a> .</p>
</li>
<li>
<p>The shell parses the input into simple commands (see <a href="#tag_02_09_01">Simple Commands</a> ) and compound commands (see <a
href="#tag_02_09_04">Compound Commands</a> ).</p>
</li>
<li>
<p>The shell performs various expansions (separately) on different parts of each command, resulting in a list of pathnames and
fields to be treated as a command and arguments; see <a href="#tag_02_06">Word Expansions</a> .</p>
</li>
<li>
<p>The shell performs redirection (see <a href="#tag_02_07">Redirection</a> ) and removes redirection operators and their operands
from the parameter list.</p>
</li>
<li>
<p>The shell executes a function (see <a href="#tag_02_09_05">Function Definition Command</a> ), built-in (see <a href=
"#tag_02_14">Special Built-In Utilities</a> ), executable file, or script, giving the names of the arguments as positional
parameters numbered 1 to <i>n</i>, and the name of the command (or in the case of a function within a script, the name of the
script) as the positional parameter numbered 0 (see <a href="#tag_02_09_01_01">Command Search and Execution</a> ).</p>
</li>
<li>
<p>The shell optionally waits for the command to complete and collects the exit status (see <a href="#tag_02_08_02">Exit Status for
Commands</a> ).</p>
</li>
</ol>
<h3><a name="tag_02_02"></a>Quoting</h3>
<p>Quoting is used to remove the special meaning of certain characters or words to the shell. Quoting can be used to preserve the
literal meaning of the special characters in the next paragraph, prevent reserved words from being recognized as such, and prevent
parameter expansion and command substitution within here-document processing (see <a href="#tag_02_07_04">Here-Document</a> ).</p>
<p>The application shall quote the following characters if they are to represent themselves:</p>
<blockquote>
<pre>
<tt>| &amp; ; &lt; &gt; ( ) $ ` \ " ' &lt;space&gt; &lt;tab&gt; &lt;newline&gt;
</tt>
</pre>
</blockquote>
<p>and the following may need to be quoted under certain circumstances. That is, these characters may be special depending on
conditions described elsewhere in this volume of IEEE&nbsp;Std&nbsp;1003.1-2001:</p>
<blockquote>
<pre>
<tt>* ? [ # &#152; = %
</tt>
</pre>
</blockquote>
<p>The various quoting mechanisms are the escape character, single-quotes, and double-quotes. The here-document represents another
form of quoting; see <a href="#tag_02_07_04">Here-Document</a> .</p>
<h4><a name="tag_02_02_01"></a>Escape Character (Backslash)</h4>
<p>A backslash that is not quoted shall preserve the literal value of the following character, with the exception of a
&lt;newline&gt;. If a &lt;newline&gt; follows the backslash, the shell shall interpret this as line continuation. The backslash and
&lt;newline&gt;s shall be removed before splitting the input into tokens. Since the escaped &lt;newline&gt; is removed entirely
from the input and is not replaced by any white space, it cannot serve as a token separator.</p>
<h4><a name="tag_02_02_02"></a>Single-Quotes</h4>
<p>Enclosing characters in single-quotes ( <tt>''</tt> ) shall preserve the literal value of each character within the
single-quotes. A single-quote cannot occur within single-quotes.</p>
<h4><a name="tag_02_02_03"></a>Double-Quotes</h4>
<p>Enclosing characters in double-quotes ( <tt>""</tt> ) shall preserve the literal value of all characters within the
double-quotes, with the exception of the characters dollar sign, backquote, and backslash, as follows:</p>
<dl compact>
<dt><tt>$</tt></dt>
<dd>The dollar sign shall retain its special meaning introducing parameter expansion (see <a href="#tag_02_06_02">Parameter
Expansion</a> ), a form of command substitution (see <a href="#tag_02_06_03">Command Substitution</a> ), and arithmetic expansion
(see <a href="#tag_02_06_04">Arithmetic Expansion</a> ).
<p>The input characters within the quoted string that are also enclosed between <tt>"$("</tt> and the matching <tt>')'</tt> shall
not be affected by the double-quotes, but rather shall define that command whose output replaces the <tt>"$(...)"</tt> when the
word is expanded. The tokenizing rules in <a href="#tag_02_03">Token Recognition</a> , not including the alias substitutions in <a
href="#tag_02_03_01">Alias Substitution</a> , shall be applied recursively to find the matching <tt>')'</tt> .</p>
<p>Within the string of characters from an enclosed <tt>"${"</tt> to the matching <tt>'}'</tt> , an even number of unescaped
double-quotes or single-quotes, if any, shall occur. A preceding backslash character shall be used to escape a literal <tt>'{'</tt>
or <tt>'}'</tt> . The rule in <a href="#tag_02_06_02">Parameter Expansion</a> shall be used to determine the matching <tt>'}'</tt>
.</p>
</dd>
<dt><tt>`</tt></dt>
<dd>The backquote shall retain its special meaning introducing the other form of command substitution (see <a href=
"#tag_02_06_03">Command Substitution</a> ). The portion of the quoted string from the initial backquote and the characters up to
the next backquote that is not preceded by a backslash, having escape characters removed, defines that command whose output
replaces <tt>"`...`"</tt> when the word is expanded. Either of the following cases produces undefined results:
<ul>
<li>
<p>A single-quoted or double-quoted string that begins, but does not end, within the <tt>"`...`"</tt> sequence</p>
</li>
<li>
<p>A <tt>"`...`"</tt> sequence that begins, but does not end, within the same double-quoted string</p>
</li>
</ul>
</dd>
<dt><tt>\</tt></dt>
<dd>The backslash shall retain its special meaning as an escape character (see <a href="#tag_02_02_01">Escape Character
(Backslash)</a> ) only when followed by one of the following characters when considered special:
<blockquote>
<pre>
<tt>$ ` " \ &lt;newline&gt;
</tt>
</pre>
</blockquote>
</dd>
</dl>
<p>The application shall ensure that a double-quote is preceded by a backslash to be included within double-quotes. The parameter
<tt>'@'</tt> has special meaning inside double-quotes and is described in <a href="#tag_02_05_02">Special Parameters</a> .</p>
<h3><a name="tag_02_03"></a>Token Recognition</h3>
<p>The shell shall read its input in terms of lines from a file, from a terminal in the case of an interactive shell, or from a
string in the case of <a href="../utilities/sh.html"><i>sh</i></a> <b>-c</b> or <a href=
"../functions/system.html"><i>system</i>()</a>. The input lines can be of unlimited length. These lines shall be parsed using two
major modes: ordinary token recognition and processing of here-documents.</p>
<p>When an <b>io_here</b> token has been recognized by the grammar (see <a href="#tag_02_10">Shell Grammar</a> ), one or more of
the subsequent lines immediately following the next <b>NEWLINE</b> token form the body of one or more here-documents and shall be
parsed according to the rules of <a href="#tag_02_07_04">Here-Document</a> .</p>
<p>When it is not processing an <b>io_here</b>, the shell shall break its input into tokens by applying the first applicable rule
below to the next character in its input. The token shall be from the current position in the input until a token is delimited
according to one of the rules below; the characters forming the token are exactly those in the input, including any quoting
characters. If it is indicated that a token is delimited, and no characters have been included in a token, processing shall
continue until an actual token is delimited.</p>
<ol>
<li>
<p>If the end of input is recognized, the current token shall be delimited. If there is no current token, the end-of-input
indicator shall be returned as the token.</p>
</li>
<li>
<p>If the previous character was used as part of an operator and the current character is not quoted and can be used with the
current characters to form an operator, it shall be used as part of that (operator) token.</p>
</li>
<li>
<p>If the previous character was used as part of an operator and the current character cannot be used with the current characters
to form an operator, the operator containing the previous character shall be delimited.</p>
</li>
<li>
<p>If the current character is backslash, single-quote, or double-quote ( <tt>'\'</tt> , <tt>'&quot;</tt> , or <tt>' )'</tt> and it is
not quoted, it shall affect quoting for subsequent characters up to the end of the quoted text. The rules for quoting are as
described in <a href="#tag_02_02">Quoting</a> . During token recognition no substitutions shall be actually performed, and the
result token shall contain exactly the characters that appear in the input (except for &lt;newline&gt; joining), unmodified,
including any embedded or enclosing quotes or substitution operators, between the quote mark and the end of the quoted text. The
token shall not be delimited by the end of the quoted field.</p>
</li>
<li>
<p>If the current character is an unquoted <tt>'$'</tt> or <tt>'`'</tt> , the shell shall identify the start of any candidates for
parameter expansion ( <a href="#tag_02_06_02">Parameter Expansion</a> ), command substitution ( <a href="#tag_02_06_03">Command
Substitution</a> ), or arithmetic expansion ( <a href="#tag_02_06_04">Arithmetic Expansion</a> ) from their introductory unquoted
character sequences: <tt>'$'</tt> or <tt>"${"</tt> , <tt>"$("</tt> or <tt>'`'</tt> , and <tt>"$(("</tt> , respectively. The shell
shall read sufficient input to determine the end of the unit to be expanded (as explained in the cited sections). While processing
the characters, if instances of expansions or quoting are found nested within the substitution, the shell shall recursively process
them in the manner specified for the construct that is found. The characters found from the beginning of the substitution to its
end, allowing for any recursion necessary to recognize embedded constructs, shall be included unmodified in the result token,
including any embedded or enclosing substitution operators or quotes. The token shall not be delimited by the end of the
substitution.</p>
</li>
<li>
<p>If the current character is not quoted and can be used as the first character of a new operator, the current token (if any)
shall be delimited. The current character shall be used as the beginning of the next (operator) token.</p>
</li>
<li>
<p>If the current character is an unquoted &lt;newline&gt;, the current token shall be delimited.</p>
</li>
<li>
<p>If the current character is an unquoted &lt;blank&gt;, any token containing the previous character is delimited and the current
character shall be discarded.</p>
</li>
<li>
<p>If the previous character was part of a word, the current character shall be appended to that word.</p>
</li>
<li>
<p>If the current character is a <tt>'#'</tt> , it and all subsequent characters up to, but excluding, the next &lt;newline&gt;
shall be discarded as a comment. The &lt;newline&gt; that ends the line is not considered part of the comment.</p>
</li>
<li>
<p>The current character is used as the start of a new word.</p>
</li>
</ol>
<p>Once a token is delimited, it is categorized as required by the grammar in <a href="#tag_02_10">Shell Grammar</a> .</p>
<h4><a name="tag_02_03_01"></a>Alias Substitution</h4>
<p><sup>[<a href="javascript:open_code('UP XSI')">UP XSI</a>]</sup> <img src="../images/opt-start.gif" alt="[Option Start]" border=
"0"> The processing of aliases shall be supported on all XSI-conformant systems or if the system supports the User Portability
Utilities option (and the rest of this section is not further marked for these options). <img src="../images/opt-end.gif" alt=
"[Option End]" border="0"></p>
<p>After a token has been delimited, but before applying the grammatical rules in <a href="#tag_02_10">Shell Grammar</a> , a
resulting word that is identified to be the command name word of a simple command shall be examined to determine whether it is an
unquoted, valid alias name. However, reserved words in correct grammatical context shall not be candidates for alias substitution.
A valid alias name (see the Base Definitions volume of IEEE&nbsp;Std&nbsp;1003.1-2001, <a href=
"../basedefs/xbd_chap03.html#tag_03_10">Section 3.10, Alias Name</a>) shall be one that has been defined by the <a href=
"../utilities/alias.html"><i>alias</i></a> utility and not subsequently undefined using <a href=
"../utilities/unalias.html"><i>unalias</i></a>. Implementations also may provide predefined valid aliases that are in effect when
the shell is invoked. To prevent infinite loops in recursive aliasing, if the shell is not currently processing an alias of the
same name, the word shall be replaced by the value of the alias; otherwise, it shall not be replaced.</p>
<p>If the value of the alias replacing the word ends in a &lt;blank&gt;, the shell shall check the next command word for alias
substitution; this process shall continue until a word is found that is not a valid alias or an alias value does not end in a
&lt;blank&gt;.</p>
<p>When used as specified by this volume of IEEE&nbsp;Std&nbsp;1003.1-2001, alias definitions shall not be inherited by separate
invocations of the shell or by the utility execution environments invoked by the shell; see <a href="#tag_02_12">Shell Execution
Environment</a> .</p>
<h3><a name="tag_02_04"></a>Reserved Words</h3>
<p>Reserved words are words that have special meaning to the shell; see <a href="#tag_02_09">Shell Commands</a> . The following
words shall be recognized as reserved words:</p>
<blockquote>
<table cellpadding="3">
<tr valign="top">
<td align="left">
<p class="tent"><b><br>
!<br>
{<br>
}<br>
case<br>
</b></p>
</td>
<td align="left">
<p class="tent"><b><br>
do<br>
done<br>
elif<br>
else<br>
</b></p>
</td>
<td align="left">
<p class="tent"><b><br>
esac<br>
fi<br>
for<br>
if<br>
</b></p>
</td>
<td align="left">
<p class="tent"><b><br>
in<br>
then<br>
until<br>
while<br>
</b></p>
</td>
</tr>
</table>
</blockquote>
<p>This recognition shall only occur when none of the characters is quoted and when the word is used as:</p>
<ul>
<li>
<p>The first word of a command</p>
</li>
<li>
<p>The first word following one of the reserved words other than <b>case</b>, <b>for</b>, or <b>in</b></p>
</li>
<li>
<p>The third word in a <b>case</b> command (only <b>in</b> is valid in this case)</p>
</li>
<li>
<p>The third word in a <b>for</b> command (only <b>in</b> and <b>do</b> are valid in this case)</p>
</li>
</ul>
<p>See the grammar in <a href="#tag_02_10">Shell Grammar</a> .</p>
<p>The following words may be recognized as reserved words on some implementations (when none of the characters are quoted),
causing unspecified results:</p>
<blockquote>
<table cellpadding="3">
<tr valign="top">
<td align="left">
<p class="tent"><b>[[</b></p>
</td>
<td align="left">
<p class="tent"><b>]]</b></p>
</td>
<td align="left">
<p class="tent"><b>function</b></p>
</td>
<td align="left">
<p class="tent"><b>select</b></p>
</td>
</tr>
</table>
</blockquote>
<p>Words that are the concatenation of a name and a colon ( <tt>':'</tt> ) are reserved; their use produces unspecified
results.</p>
<h3><a name="tag_02_05"></a>Parameters and Variables</h3>
<p>A parameter can be denoted by a name, a number, or one of the special characters listed in <a href="#tag_02_05_02">Special
Parameters</a> . A variable is a parameter denoted by a name.</p>
<p>A parameter is set if it has an assigned value (null is a valid value). Once a variable is set, it can only be unset by using
the <a href="unset.html"><i>unset</i></a> special built-in command.</p>
<h4><a name="tag_02_05_01"></a>Positional Parameters</h4>
<p>A positional parameter is a parameter denoted by the decimal value represented by one or more digits, other than the single
digit 0. The digits denoting the positional parameters shall always be interpreted as a decimal value, even if there is a leading
zero. When a positional parameter with more than one digit is specified, the application shall enclose the digits in braces (see <a
href="#tag_02_06_02">Parameter Expansion</a> ). Positional parameters are initially assigned when the shell is invoked (see <a
href="../utilities/sh.html"><i>sh</i></a>), temporarily replaced when a shell function is invoked (see <a href=
"#tag_02_09_05">Function Definition Command</a> ), and can be reassigned with the <a href="set.html"><i>set</i></a> special built-in
command.</p>
<h4><a name="tag_02_05_02"></a>Special Parameters</h4>
<p>Listed below are the special parameters and the values to which they shall expand. Only the values of the special parameters are
listed; see <a href="#tag_02_06">Word Expansions</a> for a detailed summary of all the stages involved in expanding words.</p>
<dl compact>
<dt><tt>@</tt></dt>
<dd>Expands to the positional parameters, starting from one. When the expansion occurs within double-quotes, and where field
splitting (see <a href="#tag_02_06_05">Field Splitting</a> ) is performed, each positional parameter shall expand as a separate
field, with the provision that the expansion of the first parameter shall still be joined with the beginning part of the original
word (assuming that the expanded parameter was embedded within a word), and the expansion of the last parameter shall still be
joined with the last part of the original word. If there are no positional parameters, the expansion of <tt>'@'</tt> shall generate
zero fields, even when <tt>'@'</tt> is double-quoted.</dd>
<dt><tt>*</tt></dt>
<dd>Expands to the positional parameters, starting from one. When the expansion occurs within a double-quoted string (see <a href=
"#tag_02_02_03">Double-Quotes</a> ), it shall expand to a single field with the value of each parameter separated by the first
character of the <i>IFS</i> variable, or by a &lt;space&gt; if <i>IFS</i> is unset. If <i>IFS</i> is set to a null string, this is
not equivalent to unsetting it; its first character does not exist, so the parameter values are concatenated.</dd>
<dt><tt>#</tt></dt>
<dd>Expands to the decimal number of positional parameters. The command name (parameter 0) shall not be counted in the number given
by <tt>'#'</tt> because it is a special parameter, not a positional parameter.</dd>
<dt><tt>?</tt></dt>
<dd>Expands to the decimal exit status of the most recent pipeline (see <a href="#tag_02_09_02">Pipelines</a> ).</dd>
<dt><tt>-</tt></dt>
<dd>(Hyphen.) Expands to the current option flags (the single-letter option names concatenated into a string) as specified on
invocation, by the <a href="set.html"><i>set</i></a> special built-in command, or implicitly by the shell.</dd>
<dt><tt>$</tt></dt>
<dd>Expands to the decimal process ID of the invoked shell. In a subshell (see <a href="#tag_02_12">Shell Execution Environment</a>
), <tt>'$'</tt> shall expand to the same value as that of the current shell.</dd>
<dt><tt>!</tt></dt>
<dd>Expands to the decimal process ID of the most recent background command (see <a href="#tag_02_09_03">Lists</a> ) executed from
the current shell. (For example, background commands executed from subshells do not affect the value of <tt>"$!"</tt> in the
current shell environment.) For a pipeline, the process ID is that of the last command in the pipeline.</dd>
<dt>0</dt>
<dd>(Zero.) Expands to the name of the shell or shell script. See <a href="sh.html"><i>sh</i></a> for a detailed description of how
this name is derived.</dd>
</dl>
<p>See the description of the <i>IFS</i> variable in <a href="#tag_02_05_03">Shell Variables</a> .</p>
<h4><a name="tag_02_05_03"></a>Shell Variables</h4>
<p>Variables shall be initialized from the environment (as defined by the Base Definitions volume of
IEEE&nbsp;Std&nbsp;1003.1-2001, <a href="../basedefs/xbd_chap08.html">Chapter 8, Environment Variables</a> and the <i>exec</i>
function in the System Interfaces volume of IEEE&nbsp;Std&nbsp;1003.1-2001) and can be given new values with variable assignment
commands. If a variable is initialized from the environment, it shall be marked for export immediately; see the <a href=
"export.html"><i>export</i></a> special built-in. New variables can be defined and initialized with variable assignments, with the <a
href="../utilities/read.html"><i>read</i></a> or <a href="../utilities/getopts.html"><i>getopts</i></a> utilities, with the
<i>name</i> parameter in a <b>for</b> loop, with the ${ <i>name</i>= <i>word</i>} expansion, or with other mechanisms provided as
implementation extensions.</p>
<p>The following variables shall affect the execution of the shell:</p>
<dl compact>
<dt><i>ENV</i></dt>
<dd><sup>[<a href="javascript:open_code('UP XSI')">UP XSI</a>]</sup> <img src="../images/opt-start.gif" alt="[Option Start]"
border="0"> The processing of the <i>ENV</i> shell variable shall be supported on all XSI-conformant systems or if the system
supports the User Portability Utilities option. <img src="../images/opt-end.gif" alt="[Option End]" border="0">
<p>This variable, when and only when an interactive shell is invoked, shall be subjected to parameter expansion (see <a href=
"#tag_02_06_02">Parameter Expansion</a> ) by the shell and the resulting value shall be used as a pathname of a file containing
shell commands to execute in the current environment. The file need not be executable. If the expanded value of <i>ENV</i> is not
an absolute pathname, the results are unspecified. <i>ENV</i> shall be ignored if the user's real and effective user IDs or real
and effective group IDs are different.</p>
</dd>
<dt><i>HOME</i></dt>
<dd>The pathname of the user's home directory. The contents of <i>HOME</i> are used in tilde expansion (see <a href=
"#tag_02_06_01">Tilde Expansion</a> ).</dd>
<dt><i>IFS</i></dt>
<dd>(Input Field Separators.) A string treated as a list of characters that is used for field splitting and to split lines into
fields with the <a href="../utilities/read.html"><i>read</i></a> command. If <i>IFS</i> is not set, the shell shall behave as if
the value of <i>IFS</i> is &lt;space&gt;, &lt;tab&gt;, and &lt;newline&gt;; see <a href="#tag_02_06_05">Field Splitting</a> .
Implementations may ignore the value of <i>IFS</i> in the environment at the time the shell is invoked, treating <i>IFS</i> as if
it were not set.</dd>
<dt><i>LANG</i></dt>
<dd>Provide a default value for the internationalization variables that are unset or null. (See the Base Definitions volume of
IEEE&nbsp;Std&nbsp;1003.1-2001, <a href="../basedefs/xbd_chap08.html#tag_08_02">Section 8.2, Internationalization Variables</a> for
the precedence of internationalization variables used to determine the values of locale categories.)</dd>
<dt><i>LC_ALL</i></dt>
<dd>The value of this variable overrides the <i>LC_*</i> variables and <i>LANG ,</i> as described in the Base Definitions volume of
IEEE&nbsp;Std&nbsp;1003.1-2001, <a href="../basedefs/xbd_chap08.html">Chapter 8, Environment Variables</a>.</dd>
<dt><i>LC_COLLATE</i></dt>
<dd>Determine the behavior of range expressions, equivalence classes, and multi-character collating elements within pattern
matching.</dd>
<dt><i>LC_CTYPE</i></dt>
<dd>Determine the interpretation of sequences of bytes of text data as characters (for example, single-byte as opposed to
multi-byte characters), which characters are defined as letters (character class <b>alpha</b>) and &lt;blank&gt;s (character class
<b>blank</b>), and the behavior of character classes within pattern matching. Changing the value of <i>LC_CTYPE</i> after the shell
has started shall not affect the lexical processing of shell commands in the current shell execution environment or its subshells.
Invoking a shell script or performing <a href="exec.html"><i>exec</i></a> <a href="../utilities/sh.html"><i>sh</i></a> subjects the new
shell to the changes in <i>LC_CTYPE .</i></dd>
<dt><i>LC_MESSAGES</i></dt>
<dd>Determine the language in which messages should be written.</dd>
<dt><i>LINENO</i></dt>
<dd>Set by the shell to a decimal number representing the current sequential line number (numbered starting with 1) within a script
or function before it executes each command. If the user unsets or resets <i>LINENO ,</i> the variable may lose its special meaning
for the life of the shell. If the shell is not currently executing a script or function, the value of <i>LINENO</i> is unspecified.
This volume of IEEE&nbsp;Std&nbsp;1003.1-2001 specifies the effects of the variable only for systems supporting the User
Portability Utilities option.</dd>
<dt><i>NLSPATH</i></dt>
<dd><sup>[<a href="javascript:open_code('XSI')">XSI</a>]</sup> <img src="../images/opt-start.gif" alt="[Option Start]" border="0">
Determine the location of message catalogs for the processing of <i>LC_MESSAGES .</i> <img src="../images/opt-end.gif" alt=
"[Option End]" border="0"></dd>
<dt><i>PATH</i></dt>
<dd>A string formatted as described in the Base Definitions volume of IEEE&nbsp;Std&nbsp;1003.1-2001, <a href=
"../basedefs/xbd_chap08.html">Chapter 8, Environment Variables</a>, used to effect command interpretation; see <a href=
"#tag_02_09_01_01">Command Search and Execution</a> .</dd>
<dt><i>PPID</i></dt>
<dd>Set by the shell to the decimal process ID of the process that invoked this shell. In a subshell (see <a href=
"#tag_02_12">Shell Execution Environment</a> ), <i>PPID</i> shall be set to the same value as that of the parent of the current
shell. For example, <a href="../utilities/echo.html"><i>echo</i></a> $ <i>PPID</i> and ( <a href=
"../utilities/echo.html"><i>echo</i></a> $ <i>PPID )</i> would produce the same value. This volume of
IEEE&nbsp;Std&nbsp;1003.1-2001 specifies the effects of the variable only for systems supporting the User Portability Utilities
option.</dd>
<dt><i>PS1</i></dt>
<dd>Each time an interactive shell is ready to read a command, the value of this variable shall be subjected to parameter expansion
and written to standard error. The default value shall be <tt>"$&nbsp;"</tt> . For users who have specific additional
implementation-defined privileges, the default may be another, implementation-defined value. The shell shall replace each instance
of the character <tt>'!'</tt> in <i>PS1</i> with the history file number of the next command to be typed. Escaping the <tt>'!'</tt>
with another <tt>'!'</tt> (that is, <tt>"!!"</tt> ) shall place the literal character <tt>'!'</tt> in the prompt. This volume of
IEEE&nbsp;Std&nbsp;1003.1-2001 specifies the effects of the variable only for systems supporting the User Portability Utilities
option.</dd>
<dt><i>PS2</i></dt>
<dd>Each time the user enters a &lt;newline&gt; prior to completing a command line in an interactive shell, the value of this
variable shall be subjected to parameter expansion and written to standard error. The default value is <tt>"&gt;&nbsp;"</tt> . This
volume of IEEE&nbsp;Std&nbsp;1003.1-2001 specifies the effects of the variable only for systems supporting the User Portability
Utilities option.</dd>
<dt><i>PS4</i></dt>
<dd>When an execution trace ( <a href="set.html"><i>set</i></a> <b>-x</b>) is being performed in an interactive shell, before each line
in the execution trace, the value of this variable shall be subjected to parameter expansion and written to standard error. The
default value is <tt>"+&nbsp;"</tt> . This volume of IEEE&nbsp;Std&nbsp;1003.1-2001 specifies the effects of the variable only for
systems supporting the User Portability Utilities option.</dd>
<dt><i>PWD</i></dt>
<dd>Set by the shell to be an absolute pathname of the current working directory, containing no components of type symbolic link,
no components that are dot, and no components that are dot-dot when the shell is initialized. If an application sets or unsets the
value of <i>PWD ,</i> the behaviors of the <a href="../utilities/cd.html"><i>cd</i></a> and <a href=
"../utilities/pwd.html"><i>pwd</i></a> utilities are unspecified.</dd>
</dl>
<h3><a name="tag_02_06"></a>Word Expansions</h3>
<p>This section describes the various expansions that are performed on words. Not all expansions are performed on every word, as
explained in the following sections.</p>
<p>Tilde expansions, parameter expansions, command substitutions, arithmetic expansions, and quote removals that occur within a
single word expand to a single field. It is only field splitting or pathname expansion that can create multiple fields from a
single word. The single exception to this rule is the expansion of the special parameter <tt>'@'</tt> within double-quotes, as
described in <a href="#tag_02_05_02">Special Parameters</a> .</p>
<p>The order of word expansion shall be as follows:</p>
<ol>
<li>
<p>Tilde expansion (see <a href="#tag_02_06_01">Tilde Expansion</a> ), parameter expansion (see <a href="#tag_02_06_02">Parameter
Expansion</a> ), command substitution (see <a href="#tag_02_06_03">Command Substitution</a> ), and arithmetic expansion (see <a
href="#tag_02_06_04">Arithmetic Expansion</a> ) shall be performed, beginning to end. See item 5 in <a href="#tag_02_03">Token
Recognition</a> .</p>
</li>
<li>
<p>Field splitting (see <a href="#tag_02_06_05">Field Splitting</a> ) shall be performed on the portions of the fields generated by
step 1, unless <i>IFS</i> is null.</p>
</li>
<li>
<p>Pathname expansion (see <a href="#tag_02_06_06">Pathname Expansion</a> ) shall be performed, unless <a href=
"set.html"><i>set</i></a> <b>-f</b> is in effect.</p>
</li>
<li>
<p>Quote removal (see <a href="#tag_02_06_07">Quote Removal</a> ) shall always be performed last.</p>
</li>
</ol>
<p>The expansions described in this section shall occur in the same shell environment as that in which the command is executed.</p>
<p>If the complete expansion appropriate for a word results in an empty field, that empty field shall be deleted from the list of
fields that form the completely expanded command, unless the original word contained single-quote or double-quote characters.</p>
<p>The <tt>'$'</tt> character is used to introduce parameter expansion, command substitution, or arithmetic evaluation. If an
unquoted <tt>'$'</tt> is followed by a character that is either not numeric, the name of one of the special parameters (see <a
href="#tag_02_05_02">Special Parameters</a> ), a valid first character of a variable name, a left curly brace ( <tt>'{'</tt> ) or a
left parenthesis, the result is unspecified.</p>
<h4><a name="tag_02_06_01"></a>Tilde Expansion</h4>
<p>A &quot;tilde-prefix&quot; consists of an unquoted tilde character at the beginning of a word, followed by all of the characters
preceding the first unquoted slash in the word, or all the characters in the word if there is no slash. In an assignment (see the
Base Definitions volume of IEEE&nbsp;Std&nbsp;1003.1-2001, <a href="../basedefs/xbd_chap04.html#tag_04_21">Section 4.21, Variable
Assignment</a>), multiple tilde-prefixes can be used: at the beginning of the word (that is, following the equal sign of the
assignment), following any unquoted colon, or both. A tilde-prefix in an assignment is terminated by the first unquoted colon or
slash. If none of the characters in the tilde-prefix are quoted, the characters in the tilde-prefix following the tilde are treated
as a possible login name from the user database. A portable login name cannot contain characters outside the set given in the
description of the <i>LOGNAME</i> environment variable in the Base Definitions volume of IEEE&nbsp;Std&nbsp;1003.1-2001, <a href=
"../basedefs/xbd_chap08.html#tag_08_03">Section 8.3, Other Environment Variables</a>. If the login name is null (that is, the
tilde-prefix contains only the tilde), the tilde-prefix is replaced by the value of the variable <i>HOME .</i> If <i>HOME</i> is
unset, the results are unspecified. Otherwise, the tilde-prefix shall be replaced by a pathname of the initial working directory
associated with the login name obtained using the <a href="../functions/getpwnam.html"><i>getpwnam</i>()</a> function as defined in
the System Interfaces volume of IEEE&nbsp;Std&nbsp;1003.1-2001. If the system does not recognize the login name, the results are
undefined.</p>
<h4><a name="tag_02_06_02"></a>Parameter Expansion</h4>
<p>The format for parameter expansion is as follows:</p>
<blockquote>
<pre>
<tt>${</tt><i>expression</i><tt>}
</tt>
</pre>
</blockquote>
<p>where <i>expression</i> consists of all characters until the matching <tt>'}'</tt> . Any <tt>'}'</tt> escaped by a backslash or
within a quoted string, and characters in embedded arithmetic expansions, command substitutions, and variable expansions, shall not
be examined in determining the matching <tt>'}'</tt> .</p>
<p>The simplest form for parameter expansion is:</p>
<blockquote>
<pre>
<tt>${</tt><i>parameter</i><tt>}
</tt>
</pre>
</blockquote>
<p>The value, if any, of <i>parameter</i> shall be substituted.</p>
<p>The parameter name or symbol can be enclosed in braces, which are optional except for positional parameters with more than one
digit or when <i>parameter</i> is followed by a character that could be interpreted as part of the name. The matching closing brace
shall be determined by counting brace levels, skipping over enclosed quoted strings, and command substitutions.</p>
<p>If the parameter name or symbol is not enclosed in braces, the expansion shall use the longest valid name (see the Base
Definitions volume of IEEE&nbsp;Std&nbsp;1003.1-2001, <a href="../basedefs/xbd_chap03.html#tag_03_230">Section 3.230, Name</a>),
whether or not the symbol represented by that name exists.</p>
<p>If a parameter expansion occurs inside double-quotes:</p>
<ul>
<li>
<p>Pathname expansion shall not be performed on the results of the expansion.</p>
</li>
<li>
<p>Field splitting shall not be performed on the results of the expansion, with the exception of <tt>'@'</tt> ; see <a href=
"#tag_02_05_02">Special Parameters</a> .</p>
</li>
</ul>
<p>In addition, a parameter expansion can be modified by using one of the following formats. In each case that a value of
<i>word</i> is needed (based on the state of <i>parameter</i>, as described below), <i>word</i> shall be subjected to tilde
expansion, parameter expansion, command substitution, and arithmetic expansion. If <i>word</i> is not needed, it shall not be
expanded. The <tt>'}'</tt> character that delimits the following parameter expansion modifications shall be determined as described
previously in this section and in <a href="#tag_02_02_03">Double-Quotes</a> . (For example, ${ <b>foo-bar</b>} <b>xyz</b>} would
result in the expansion of <b>foo</b> followed by the string <b>xyz</b>} if <b>foo</b> is set, else the string <tt>"barxyz}"</tt>
).</p>
<dl compact>
<dt>${<i>parameter</i>:-<i>word</i>}</dt>
<dd><b>Use Default Values</b>. If <i>parameter</i> is unset or null, the expansion of <i>word</i> shall be substituted; otherwise,
the value of <i>parameter</i> shall be substituted.</dd>
<dt>${<i>parameter</i>:=<i>word</i>}</dt>
<dd><b>Assign Default Values</b>. If <i>parameter</i> is unset or null, the expansion of <i>word</i> shall be assigned to
<i>parameter</i>. In all cases, the final value of <i>parameter</i> shall be substituted. Only variables, not positional parameters
or special parameters, can be assigned in this way.</dd>
<dt>${<i>parameter</i>:?<b>[</b><i>word</i><b>]</b>}</dt>
<dd><b>Indicate Error if Null or Unset</b>. If <i>parameter</i> is unset or null, the expansion of <i>word</i> (or a message
indicating it is unset if <i>word</i> is omitted) shall be written to standard error and the shell exits with a non-zero exit
status. Otherwise, the value of <i>parameter</i> shall be substituted. An interactive shell need not exit.</dd>
<dt>${<i>parameter</i>:+<i>word</i>}</dt>
<dd><b>Use Alternative Value</b>. If <i>parameter</i> is unset or null, null shall be substituted; otherwise, the expansion of
<i>word</i> shall be substituted.</dd>
</dl>
<p>In the parameter expansions shown previously, use of the colon in the format shall result in a test for a parameter that is
unset or null; omission of the colon shall result in a test for a parameter that is only unset. The following table summarizes the
effect of the colon:</p>
<center>
<table border="1" cellpadding="3" align="center">
<tr valign="top">
<th align="center">
<p class="tent"><i>&nbsp;</i></p>
</th>
<th align="center">
<p class="tent"><i>parameter</i></p>
</th>
<th align="center">
<p class="tent"><i>parameter</i></p>
</th>
<th align="center">
<p class="tent"><i>parameter</i></p>
</th>
</tr>
<tr valign="top">
<th align="center">
<p class="tent"><b>&nbsp;</b></p>
</th>
<th align="center">
<p class="tent"><b>Set and Not Null</b></p>
</th>
<th align="center">
<p class="tent"><b>Set But Null</b></p>
</th>
<th align="center">
<p class="tent"><b>Unset</b></p>
</th>
</tr>
<tr valign="top">
<td align="left">
<p class="tent"><b>${</b><i>parameter</i><b>:-</b><i>word</i><b>}</b></p>
</td>
<td align="left">
<p class="tent">substitute <i>parameter</i></p>
</td>
<td align="left">
<p class="tent">substitute <i>word</i></p>
</td>
<td align="left">
<p class="tent">substitute <i>word</i></p>
</td>
</tr>
<tr valign="top">
<td align="left">
<p class="tent"><b>${</b><i>parameter</i><b>-</b><i>word</i><b>}</b></p>
</td>
<td align="left">
<p class="tent">substitute <i>parameter</i></p>
</td>
<td align="left">
<p class="tent">substitute null</p>
</td>
<td align="left">
<p class="tent">substitute <i>word</i></p>
</td>
</tr>
<tr valign="top">
<td align="left">
<p class="tent"><b>${</b><i>parameter</i><b>:=</b><i>word</i><b>}</b></p>
</td>
<td align="left">
<p class="tent">substitute <i>parameter</i></p>
</td>
<td align="left">
<p class="tent">assign <i>word</i></p>
</td>
<td align="left">
<p class="tent">assign <i>word</i></p>
</td>
</tr>
<tr valign="top">
<td align="left">
<p class="tent"><b>${</b><i>parameter</i><b>=</b><i>word</i><b>}</b></p>
</td>
<td align="left">
<p class="tent">substitute <i>parameter</i></p>
</td>
<td align="left">
<p class="tent">substitute null</p>
</td>
<td align="left">
<p class="tent">assign <i>word</i></p>
</td>
</tr>
<tr valign="top">
<td align="left">
<p class="tent"><b>${</b><i>parameter</i><b>:?</b><i>word</i><b>}</b></p>
</td>
<td align="left">
<p class="tent">substitute <i>parameter</i></p>
</td>
<td align="left">
<p class="tent">error, exit</p>
</td>
<td align="left">
<p class="tent">error, exit</p>
</td>
</tr>
<tr valign="top">
<td align="left">
<p class="tent"><b>${</b><i>parameter</i><b>?</b><i>word</i><b>}</b></p>
</td>
<td align="left">
<p class="tent">substitute <i>parameter</i></p>
</td>
<td align="left">
<p class="tent">substitute null</p>
</td>
<td align="left">
<p class="tent">error, exit</p>
</td>
</tr>
<tr valign="top">
<td align="left">
<p class="tent"><b>${</b><i>parameter</i><b>:+</b><i>word</i><b>}</b></p>
</td>
<td align="left">
<p class="tent">substitute <i>word</i></p>
</td>
<td align="left">
<p class="tent">substitute null</p>
</td>
<td align="left">
<p class="tent">substitute null</p>
</td>
</tr>
<tr valign="top">
<td align="left">
<p class="tent"><b>${</b><i>parameter</i><b>+</b><i>word</i><b>}</b></p>
</td>
<td align="left">
<p class="tent">substitute <i>word</i></p>
</td>
<td align="left">
<p class="tent">substitute <i>word</i></p>
</td>
<td align="left">
<p class="tent">substitute null</p>
</td>
</tr>
</table>
</center>
<p>In all cases shown with &quot;substitute&quot;, the expression is replaced with the value shown. In all cases shown with &quot;assign&quot;,
<i>parameter</i> is assigned that value, which also replaces the expression.</p>
<dl compact>
<dt>${#<i>parameter</i>}</dt>
<dd><b>String Length</b>. The length in characters of the value of <i>parameter</i> shall be substituted. If <i>parameter</i> is
<tt>'*'</tt> or <tt>'@'</tt> , the result of the expansion is unspecified.</dd>
</dl>
<p>The following four varieties of parameter expansion provide for substring processing. In each case, pattern matching notation
(see <a href="#tag_02_13">Pattern Matching Notation</a> ), rather than regular expression notation, shall be used to evaluate the
patterns. If <i>parameter</i> is <tt>'*'</tt> or <tt>'@'</tt> , the result of the expansion is unspecified. Enclosing the full
parameter expansion string in double-quotes shall not cause the following four varieties of pattern characters to be quoted,
whereas quoting characters within the braces shall have this effect.</p>
<dl compact>
<dt>${<i>parameter</i>%<i>word</i>}</dt>
<dd><b>Remove Smallest Suffix Pattern</b>. The <i>word</i> shall be expanded to produce a pattern. The parameter expansion shall
then result in <i>parameter</i>, with the smallest portion of the suffix matched by the <i>pattern</i> deleted.</dd>
<dt>${<i>parameter</i>%%<i>word</i>}</dt>
<dd><b>Remove Largest Suffix Pattern</b>. The <i>word</i> shall be expanded to produce a pattern. The parameter expansion shall
then result in <i>parameter</i>, with the largest portion of the suffix matched by the <i>pattern</i> deleted.</dd>
<dt>${<i>parameter</i>#<i>word</i>}</dt>
<dd><b>Remove Smallest Prefix Pattern</b>. The <i>word</i> shall be expanded to produce a pattern. The parameter expansion shall
then result in <i>parameter</i>, with the smallest portion of the prefix matched by the <i>pattern</i> deleted.</dd>
<dt>${<i>parameter</i>##<i>word</i>}</dt>
<dd><b>Remove Largest Prefix Pattern</b>. The <i>word</i> shall be expanded to produce a pattern. The parameter expansion shall
then result in <i>parameter</i>, with the largest portion of the prefix matched by the <i>pattern</i> deleted.</dd>
</dl>
<hr>
<div class="box"><em>The following sections are informative.</em></div>
<h5><a name="tag_02_06_02_01"></a>Examples</h5>
<dl compact>
<dt>${<i>parameter</i>:-<i>word</i>}</dt>
<dd><br>
In this example, <a href="../utilities/ls.html"><i>ls</i></a> is executed only if <i>x</i> is null or unset. (The $( <a href=
"../utilities/ls.html"><i>ls</i></a>) command substitution notation is explained in <a href="#tag_02_06_03">Command
Substitution</a> .)
<blockquote>
<pre>
<tt>${x:-$(ls)}
</tt>
</pre>
</blockquote>
</dd>
<dt>${<i>parameter</i>:=<i>word</i>}</dt>
<dd>
<pre>
<tt>unset X
echo ${X:=abc}
</tt><b>abc</b>
</pre>
</dd>
<dt>${<i>parameter</i>:?<i>word</i>}</dt>
<dd>
<pre>
<tt>unset posix
echo ${posix:?}
</tt><b>sh: posix: parameter null or not set</b>
</pre>
</dd>
<dt>${<i>parameter</i>:+<i>word</i>}</dt>
<dd>
<pre>
<tt>set a b c
echo ${3:+posix}
</tt><b>posix</b>
</pre>
</dd>
<dt>${#<i>parameter</i>}</dt>
<dd>
<pre>
<tt>HOME=/usr/posix
echo ${#HOME}
</tt><b>10</b>
</pre>
</dd>
<dt>${<i>parameter</i>%<i>word</i>}</dt>
<dd>
<pre>
<tt>x=file.c
echo ${x%.c}.o
</tt><b>file.o</b>
</pre>
</dd>
<dt>${<i>parameter</i>%%<i>word</i>}</dt>
<dd>
<pre>
<tt>x=posix/src/std
echo ${x%%/*}
</tt><b>posix</b>
</pre>
</dd>
<dt>${<i>parameter</i>#<i>word</i>}</dt>
<dd>
<pre>
<tt>x=$HOME/src/cmd
echo ${x#$HOME}
</tt><b>/src/cmd</b>
</pre>
</dd>
<dt>${<i>parameter</i>##<i>word</i>}</dt>
<dd>
<pre>
<tt>x=/one/two/three
echo ${x##*/}
</tt><b>three</b>
</pre>
</dd>
</dl>
<p>The double-quoting of patterns is different depending on where the double-quotes are placed:</p>
<dl compact>
<dt><tt>"${x#*}"</tt></dt>
<dd>The asterisk is a pattern character.</dd>
<dt><tt>${x#"*"}</tt></dt>
<dd>The literal asterisk is quoted and not special.</dd>
</dl>
<div class="box"><em>End of informative text.</em></div>
<hr>
<h4><a name="tag_02_06_03"></a>Command Substitution</h4>
<p>Command substitution allows the output of a command to be substituted in place of the command name itself. Command substitution
shall occur when the command is enclosed as follows:</p>
<blockquote>
<pre>
<tt>$(</tt><i>command</i><tt>)
</tt>
</pre>
</blockquote>
<p>or (backquoted version):</p>
<blockquote>
<pre>
<tt>`</tt><i>command</i><tt>`
</tt>
</pre>
</blockquote>
<p>The shell shall expand the command substitution by executing <i>command</i> in a subshell environment (see <a href=
"#tag_02_12">Shell Execution Environment</a> ) and replacing the command substitution (the text of <i>command</i> plus the
enclosing <tt>"$()"</tt> or backquotes) with the standard output of the command, removing sequences of one or more &lt;newline&gt;s
at the end of the substitution. Embedded &lt;newline&gt;s before the end of the output shall not be removed; however, they may be
treated as field delimiters and eliminated during field splitting, depending on the value of <i>IFS</i> and quoting that is in
effect.</p>
<p>Within the backquoted style of command substitution, backslash shall retain its literal meaning, except when followed by:
<tt>'$'</tt> , <tt>'`'</tt> , or <tt>'\'</tt> (dollar sign, backquote, backslash). The search for the matching backquote shall be
satisfied by the first backquote found without a preceding backslash; during this search, if a non-escaped backquote is encountered
within a shell comment, a here-document, an embedded command substitution of the $( <i>command</i>) form, or a quoted string,
undefined results occur. A single-quoted or double-quoted string that begins, but does not end, within the <tt>"`...`"</tt>
sequence produces undefined results.</p>
<p>With the $( <i>command</i>) form, all characters following the open parenthesis to the matching closing parenthesis constitute
the <i>command</i>. Any valid shell script can be used for <i>command</i>, except a script consisting solely of redirections which
produces unspecified results.</p>
<p>The results of command substitution shall not be processed for further tilde expansion, parameter expansion, command
substitution, or arithmetic expansion. If a command substitution occurs inside double-quotes, it shall not be performed on the
results of the substitution.</p>
<p>Command substitution can be nested. To specify nesting within the backquoted version, the application shall precede the inner
backquotes with backslashes, for example:</p>
<blockquote>
<pre>
<tt>\`</tt><i>command</i><tt>\`
</tt>
</pre>
</blockquote>
<p>If the command substitution consists of a single subshell, such as:</p>
<blockquote>
<pre>
<tt>$( (</tt><i>command</i><tt>) )
</tt>
</pre>
</blockquote>
<p>a conforming application shall separate the <tt>"$("</tt> and <tt>'('</tt> into two tokens (that is, separate them with white
space). This is required to avoid any ambiguities with arithmetic expansion.</p>
<h4><a name="tag_02_06_04"></a>Arithmetic Expansion</h4>
<p>Arithmetic expansion provides a mechanism for evaluating an arithmetic expression and substituting its value. The format for
arithmetic expansion shall be as follows:</p>
<blockquote>
<pre>
<tt>$((</tt><i>expression</i><tt>))
</tt>
</pre>
</blockquote>
<p>The expression shall be treated as if it were in double-quotes, except that a double-quote inside the expression is not treated
specially. The shell shall expand all tokens in the expression for parameter expansion, command substitution, and quote
removal.</p>
<p>Next, the shell shall treat this as an arithmetic expression and substitute the value of the expression. The arithmetic
expression shall be processed according to the rules given in <a href="xcu_chap01.html#tag_01_07_02_01"><i>Arithmetic Precision and
Operations</i></a> , with the following exceptions:</p>
<ul>
<li>
<p>Only signed long integer arithmetic is required.</p>
</li>
<li>
<p>Only the decimal-constant, octal-constant, and hexadecimal-constant constants specified in the ISO&nbsp;C standard, Section
6.4.4.1 are required to be recognized as constants.</p>
</li>
<li>
<p>The <i>sizeof</i>() operator and the prefix and postfix <tt>"++"</tt> and <tt>"--"</tt> operators are not required.</p>
</li>
<li>
<p>Selection, iteration, and jump statements are not supported.</p>
</li>
</ul>
<p>As an extension, the shell may recognize arithmetic expressions beyond those listed. The shell may use a signed integer type
with a rank larger than the rank of <b>signed long</b>. The shell may use a real-floating type instead of <b>signed long</b> as
long as it does not affect the results in cases where there is no overflow. If the expression is invalid, the expansion fails and
the shell shall write a message to standard error indicating the failure.</p>
<hr>
<div class="box"><em>The following sections are informative.</em></div>
<h5><a name="tag_02_06_04_01"></a>Examples</h5>
<p>A simple example using arithmetic expansion:</p>
<blockquote>
<pre>
<tt># repeat a command 100 times
x=100
while [ $x -gt 0 ]
do
</tt> <i>command</i> <tt> x=$(($x-1))
done
</tt>
</pre>
</blockquote>
<div class="box"><em>End of informative text.</em></div>
<hr>
<h4><a name="tag_02_06_05"></a>Field Splitting</h4>
<p>After parameter expansion ( <a href="#tag_02_06_02">Parameter Expansion</a> ), command substitution ( <a href=
"#tag_02_06_03">Command Substitution</a> ), and arithmetic expansion ( <a href="#tag_02_06_04">Arithmetic Expansion</a> ), the
shell shall scan the results of expansions and substitutions that did not occur in double-quotes for field splitting and multiple
fields can result.</p>
<p>The shell shall treat each character of the <i>IFS</i> as a delimiter and use the delimiters to split the results of parameter
expansion and command substitution into fields.</p>
<ol>
<li>
<p>If the value of <i>IFS</i> is a &lt;space&gt;, &lt;tab&gt;, and &lt;newline&gt;, or if it is unset, any sequence of
&lt;space&gt;s, &lt;tab&gt;s, or &lt;newline&gt;s at the beginning or end of the input shall be ignored and any sequence of those
characters within the input shall delimit a field. For example, the input:</p>
<blockquote>
<pre>
<tt>&lt;newline&gt;&lt;space&gt;&lt;tab&gt;foo&lt;tab&gt;&lt;tab&gt;bar&lt;space&gt;
</tt>
</pre>
</blockquote>
<p>yields two fields, <b>foo</b> and <b>bar</b>.</p>
</li>
<li>
<p>If the value of <i>IFS</i> is null, no field splitting shall be performed.</p>
</li>
<li>
<p>Otherwise, the following rules shall be applied in sequence. The term &quot; <i>IFS</i> white space&quot; is used to mean any sequence
(zero or more instances) of white space characters that are in the <i>IFS</i> value (for example, if <i>IFS</i> contains
&lt;space&gt;/ &lt;comma&gt;/ &lt;tab&gt;, any sequence of &lt;space&gt;s and &lt;tab&gt;s is considered <i>IFS</i> white
space).</p>
<ol type="a">
<li>
<p><i>IFS</i> white space shall be ignored at the beginning and end of the input.</p>
</li>
<li>
<p>Each occurrence in the input of an <i>IFS</i> character that is not <i>IFS</i> white space, along with any adjacent <i>IFS</i>
white space, shall delimit a field, as described previously.</p>
</li>
<li>
<p>Non-zero-length <i>IFS</i> white space shall delimit a field.</p>
</li>
</ol>
</li>
</ol>
<h4><a name="tag_02_06_06"></a>Pathname Expansion</h4>
<p>After field splitting, if <a href="set.html"><i>set</i></a> <b>-f</b> is not in effect, each field in the resulting command line
shall be expanded using the algorithm described in <a href="#tag_02_13">Pattern Matching Notation</a> , qualified by the rules in
<a href="#tag_02_13_03">Patterns Used for Filename Expansion</a> .</p>
<h4><a name="tag_02_06_07"></a>Quote Removal</h4>
<p>The quote characters: <tt>'\'</tt> , <tt>'&quot;</tt> , and <tt>''</tt> (backslash, single-quote, double-quote) that were present in
the original word shall be removed unless they have themselves been quoted.</p>
<h3><a name="tag_02_07"></a>Redirection</h3>
<p>Redirection is used to open and close files for the current shell execution environment (see <a href="#tag_02_12">Shell
Execution Environment</a> ) or for any command. Redirection operators can be used with numbers representing file descriptors (see
the Base Definitions volume of IEEE&nbsp;Std&nbsp;1003.1-2001, <a href="../basedefs/xbd_chap03.html#tag_03_165">Section 3.165, File
Descriptor</a>) as described below.</p>
<p>The overall format used for redirection is:</p>
<blockquote>
<pre>
<b>[</b><i>n</i><b>]</b><i>redir-op word</i>
</pre>
</blockquote>
<p>The number <i>n</i> is an optional decimal number designating the file descriptor number; the application shall ensure it is
delimited from any preceding text and immediately precede the redirection operator <i>redir-op</i>. If <i>n</i> is quoted, the
number shall not be recognized as part of the redirection expression. For example:</p>
<blockquote>
<pre>
<tt>echo \2&gt;a
</tt>
</pre>
</blockquote>
<p>writes the character 2 into file <b>a</b>. If any part of <i>redir-op</i> is quoted, no redirection expression is recognized.
For example:</p>
<blockquote>
<pre>
<tt>echo 2\&gt;a
</tt>
</pre>
</blockquote>
<p>writes the characters 2&gt;<i>a</i> to standard output. The optional number, redirection operator, and <i>word</i> shall not
appear in the arguments provided to the command to be executed (if any).</p>
<p>Open files are represented by decimal numbers starting with zero. The largest possible value is implementation-defined; however,
all implementations shall support at least 0 to 9, inclusive, for use by the application. These numbers are called &quot;file
descriptors&quot;. The values 0, 1, and 2 have special meaning and conventional uses and are implied by certain redirection operations;
they are referred to as <i>standard input</i>, <i>standard output</i>, and <i>standard error</i>, respectively. Programs usually
take their input from standard input, and write output on standard output. Error messages are usually written on standard error.
The redirection operators can be preceded by one or more digits (with no intervening &lt;blank&gt;s allowed) to designate the file
descriptor number.</p>
<p>If the redirection operator is <tt>"&lt;&lt;"</tt> or <tt>"&lt;&lt;-"</tt> , the word that follows the redirection operator
shall be subjected to quote removal; it is unspecified whether any of the other expansions occur. For the other redirection
operators, the word that follows the redirection operator shall be subjected to tilde expansion, parameter expansion, command
substitution, arithmetic expansion, and quote removal. Pathname expansion shall not be performed on the word by a non-interactive
shell; an interactive shell may perform it, but shall do so only when the expansion would result in one word.</p>
<p>If more than one redirection operator is specified with a command, the order of evaluation is from beginning to end.</p>
<p>A failure to open or create a file shall cause a redirection to fail.</p>
<h4><a name="tag_02_07_01"></a>Redirecting Input</h4>
<p>Input redirection shall cause the file whose name results from the expansion of <i>word</i> to be opened for reading on the
designated file descriptor, or standard input if the file descriptor is not specified.</p>
<p>The general format for redirecting input is:</p>
<blockquote>
<pre>
<b>[</b><i>n</i><b>]</b><tt>&lt;</tt><i>word</i>
</pre>
</blockquote>
<p>where the optional <i>n</i> represents the file descriptor number. If the number is omitted, the redirection shall refer to
standard input (file descriptor 0).</p>
<h4><a name="tag_02_07_02"></a>Redirecting Output</h4>
<p>The two general formats for redirecting output are:</p>
<blockquote>
<pre>
<b>[</b><i>n</i><b>]</b><tt>&gt;</tt><i>word</i>
<b>[</b><i>n</i><b>]</b><tt>&gt;|</tt><i>word</i>
</pre>
</blockquote>
<p>where the optional <i>n</i> represents the file descriptor number. If the number is omitted, the redirection shall refer to
standard output (file descriptor 1).</p>
<p>Output redirection using the <tt>'&gt;'</tt> format shall fail if the <i>noclobber</i> option is set (see the description of <a
href="set.html"><i>set</i></a> <b>-C</b>) and the file named by the expansion of <i>word</i> exists and is a regular file. Otherwise,
redirection using the <tt>'&gt;'</tt> or <tt>"&gt;|"</tt> formats shall cause the file whose name results from the expansion of
<i>word</i> to be created and opened for output on the designated file descriptor, or standard output if none is specified. If the
file does not exist, it shall be created; otherwise, it shall be truncated to be an empty file after being opened.</p>
<h4><a name="tag_02_07_03"></a>Appending Redirected Output</h4>
<p>Appended output redirection shall cause the file whose name results from the expansion of word to be opened for output on the
designated file descriptor. The file is opened as if the <a href="../functions/open.html"><i>open</i>()</a> function as defined in
the System Interfaces volume of IEEE&nbsp;Std&nbsp;1003.1-2001 was called with the O_APPEND flag. If the file does not exist, it
shall be created.</p>
<p>The general format for appending redirected output is as follows:</p>
<blockquote>
<pre>
<b>[</b><i>n</i><b>]</b><tt>&gt;&gt;</tt><i>word</i>
</pre>
</blockquote>
<p>where the optional <i>n</i> represents the file descriptor number. If the number is omitted, the redirection refers to standard
output (file descriptor 1).</p>
<h4><a name="tag_02_07_04"></a>Here-Document</h4>
<p>The redirection operators <tt>"&lt;&lt;"</tt> and <tt>"&lt;&lt;-"</tt> both allow redirection of lines contained in a shell
input file, known as a &quot;here-document&quot;, to the input of a command.</p>
<p>The here-document shall be treated as a single word that begins after the next &lt;newline&gt; and continues until there is a
line containing only the delimiter and a &lt;newline&gt;, with no &lt;blank&gt;s in between. Then the next here-document starts, if
there is one. The format is as follows:</p>
<blockquote>
<pre>
<b>[</b><i>n</i><b>]</b><tt>&lt;&lt;</tt><i>word
here-document
delimiter</i>
</pre>
</blockquote>
<p>where the optional <i>n</i> represents the file descriptor number. If the number is omitted, the here-document refers to
standard input (file descriptor 0).</p>
<p>If any character in <i>word</i> is quoted, the delimiter shall be formed by performing quote removal on <i>word</i>, and the
here-document lines shall not be expanded. Otherwise, the delimiter shall be the <i>word</i> itself.</p>
<p>If no characters in <i>word</i> are quoted, all lines of the here-document shall be expanded for parameter expansion, command
substitution, and arithmetic expansion. In this case, the backslash in the input behaves as the backslash inside double-quotes (see
<a href="#tag_02_02_03">Double-Quotes</a> ). However, the double-quote character ( <tt>' )'</tt> shall not be treated specially
within a here-document, except when the double-quote appears within <tt>"$()"</tt> , <tt>"``"</tt> , or <tt>"${}"</tt> .</p>
<p>If the redirection symbol is <tt>"&lt;&lt;-"</tt> , all leading &lt;tab&gt;s shall be stripped from input lines and the line
containing the trailing delimiter. If more than one <tt>"&lt;&lt;"</tt> or <tt>"&lt;&lt;-"</tt> operator is specified on a line,
the here-document associated with the first operator shall be supplied first by the application and shall be read first by the
shell.</p>
<hr>
<div class="box"><em>The following sections are informative.</em></div>
<h5><a name="tag_02_07_04_01"></a>Examples</h5>
<p>An example of a here-document follows:</p>
<blockquote>
<pre>
<tt>cat &lt;&lt;eof1; cat &lt;&lt;eof2
Hi,
eof1
Helene.
eof2
</tt>
</pre>
</blockquote>
<div class="box"><em>End of informative text.</em></div>
<hr>
<h4><a name="tag_02_07_05"></a>Duplicating an Input File Descriptor</h4>
<p>The redirection operator:</p>
<blockquote>
<pre>
<b>[</b><i>n</i><b>]</b><tt>&lt;&amp;</tt><i>word</i>
</pre>
</blockquote>
<p>shall duplicate one input file descriptor from another, or shall close one. If <i>word</i> evaluates to one or more digits, the
file descriptor denoted by <i>n</i>, or standard input if <i>n</i> is not specified, shall be made to be a copy of the file
descriptor denoted by <i>word</i>; if the digits in <i>word</i> do not represent a file descriptor already open for input, a
redirection error shall result; see <a href="#tag_02_08_01">Consequences of Shell Errors</a> . If <i>word</i> evaluates to
<tt>'-'</tt> , file descriptor <i>n</i>, or standard input if <i>n</i> is not specified, shall be closed. Attempts to close a file
descriptor that is not open shall not constitute an error. If <i>word</i> evaluates to something else, the behavior is
unspecified.</p>
<h4><a name="tag_02_07_06"></a>Duplicating an Output File Descriptor</h4>
<p>The redirection operator:</p>
<blockquote>
<pre>
<b>[</b><i>n</i><b>]</b><tt>&gt;&amp;</tt><i>word</i>
</pre>
</blockquote>
<p>shall duplicate one output file descriptor from another, or shall close one. If <i>word</i> evaluates to one or more digits, the
file descriptor denoted by <i>n</i>, or standard output if <i>n</i> is not specified, shall be made to be a copy of the file
descriptor denoted by <i>word</i>; if the digits in <i>word</i> do not represent a file descriptor already open for output, a
redirection error shall result; see <a href="#tag_02_08_01">Consequences of Shell Errors</a> . If <i>word</i> evaluates to
<tt>'-'</tt> , file descriptor <i>n</i>, or standard output if <i>n</i> is not specified, is closed. Attempts to close a file
descriptor that is not open shall not constitute an error. If <i>word</i> evaluates to something else, the behavior is
unspecified.</p>
<h4><a name="tag_02_07_07"></a>Open File Descriptors for Reading and Writing</h4>
<p>The redirection operator:</p>
<blockquote>
<pre>
<b>[</b><i>n</i><b>]</b><tt>&lt;&gt;</tt><i>word</i>
</pre>
</blockquote>
<p>shall cause the file whose name is the expansion of <i>word</i> to be opened for both reading and writing on the file descriptor
denoted by <i>n</i>, or standard input if <i>n</i> is not specified. If the file does not exist, it shall be created.</p>
<h3><a name="tag_02_08"></a>Exit Status and Errors</h3>
<h4><a name="tag_02_08_01"></a>Consequences of Shell Errors</h4>
<p>For a non-interactive shell, an error condition encountered by a special built-in (see <a href="#tag_02_14">Special Built-In
Utilities</a> ) or other type of utility shall cause the shell to write a diagnostic message to standard error and exit as shown in
the following table:</p>
<center>
<table border="1" cellpadding="3" align="center">
<tr valign="top">
<th align="center">
<p class="tent"><b>Error</b></p>
</th>
<th align="center">
<p class="tent"><b>Special Built-In</b></p>
</th>
<th align="center">
<p class="tent"><b>Other Utilities</b></p>
</th>
</tr>
<tr valign="top">
<td align="left">
<p class="tent">Shell language syntax error</p>
</td>
<td align="left">
<p class="tent">Shall exit</p>
</td>
<td align="left">
<p class="tent">Shall exit</p>
</td>
</tr>
<tr valign="top">
<td align="left">
<p class="tent">Utility syntax error (option or operand error)</p>
</td>
<td align="left">
<p class="tent">Shall exit</p>
</td>
<td align="left">
<p class="tent">Shall not exit</p>
</td>
</tr>
<tr valign="top">
<td align="left">
<p class="tent">Redirection error</p>
</td>
<td align="left">
<p class="tent">Shall exit</p>
</td>
<td align="left">
<p class="tent">Shall not exit</p>
</td>
</tr>
<tr valign="top">
<td align="left">
<p class="tent">Variable assignment error</p>
</td>
<td align="left">
<p class="tent">Shall exit</p>
</td>
<td align="left">
<p class="tent">Shall not exit</p>
</td>
</tr>
<tr valign="top">
<td align="left">
<p class="tent">Expansion error</p>
</td>
<td align="left">
<p class="tent">Shall exit</p>
</td>
<td align="left">
<p class="tent">Shall exit</p>
</td>
</tr>
<tr valign="top">
<td align="left">
<p class="tent">Command not found</p>
</td>
<td align="left">
<p class="tent">N/A</p>
</td>
<td align="left">
<p class="tent">May exit</p>
</td>
</tr>
<tr valign="top">
<td align="left">
<p class="tent">Dot script not found</p>
</td>
<td align="left">
<p class="tent">Shall exit</p>
</td>
<td align="left">
<p class="tent">N/A</p>
</td>
</tr>
</table>
</center>
<p>An expansion error is one that occurs when the shell expansions defined in <a href="#tag_02_06">Word Expansions</a> are carried
out (for example, <tt>"${x!y}"</tt> , because <tt>'!'</tt> is not a valid operator); an implementation may treat these as syntax
errors if it is able to detect them during tokenization, rather than during expansion.</p>
<p>If any of the errors shown as &quot;shall exit&quot; or &quot;(may) exit&quot; occur in a subshell, the subshell shall (respectively may) exit
with a non-zero status, but the script containing the subshell shall not exit because of the error.</p>
<p>In all of the cases shown in the table, an interactive shell shall write a diagnostic message to standard error without
exiting.</p>
<h4><a name="tag_02_08_02"></a>Exit Status for Commands</h4>
<p>Each command has an exit status that can influence the behavior of other shell commands. The exit status of commands that are
not utilities is documented in this section. The exit status of the standard utilities is documented in their respective
sections.</p>
<p>If a command is not found, the exit status shall be 127. If the command name is found, but it is not an executable utility, the
exit status shall be 126. Applications that invoke utilities without using the shell should use these exit status values to report
similar errors.</p>
<p>If a command fails during word expansion or redirection, its exit status shall be greater than zero.</p>
<p>Internally, for purposes of deciding whether a command exits with a non-zero exit status, the shell shall recognize the entire
status value retrieved for the command by the equivalent of the <a href="../functions/wait.html"><i>wait</i>()</a> function
WEXITSTATUS macro (as defined in the System Interfaces volume of IEEE&nbsp;Std&nbsp;1003.1-2001). When reporting the exit status
with the special parameter <tt>'?'</tt> , the shell shall report the full eight bits of exit status available. The exit status of a
command that terminated because it received a signal shall be reported as greater than 128.</p>
<h3><a name="tag_02_09"></a>Shell Commands</h3>
<p>This section describes the basic structure of shell commands. The following command descriptions each describe a format of the
command that is only used to aid the reader in recognizing the command type, and does not formally represent the syntax. Each
description discusses the semantics of the command; for a formal definition of the command language, consult <a href=
"#tag_02_10">Shell Grammar</a> .</p>
<p>A <i>command</i> is one of the following:</p>
<ul>
<li>
<p>Simple command (see <a href="#tag_02_09_01">Simple Commands</a> )</p>
</li>
<li>
<p>Pipeline (see <a href="#tag_02_09_02">Pipelines</a> )</p>
</li>
<li>
<p>List compound-list (see <a href="#tag_02_09_03">Lists</a> )</p>
</li>
<li>
<p>Compound command (see <a href="#tag_02_09_04">Compound Commands</a> )</p>
</li>
<li>
<p>Function definition (see <a href="#tag_02_09_05">Function Definition Command</a> )</p>
</li>
</ul>
<p>Unless otherwise stated, the exit status of a command shall be that of the last simple command executed by the command. There
shall be no limit on the size of any shell command other than that imposed by the underlying system (memory constraints, {ARG_MAX},
and so on).</p>
<h4><a name="tag_02_09_01"></a>Simple Commands</h4>
<p>A &quot;simple command&quot; is a sequence of optional variable assignments and redirections, in any sequence, optionally followed by
words and redirections, terminated by a control operator.</p>
<p>When a given simple command is required to be executed (that is, when any conditional construct such as an AND-OR list or a
<b>case</b> statement has not bypassed the simple command), the following expansions, assignments, and redirections shall all be
performed from the beginning of the command text to the end:</p>
<ol>
<li>
<p>The words that are recognized as variable assignments or redirections according to <a href="#tag_02_10_02">Shell Grammar
Rules</a> are saved for processing in steps 3 and 4.</p>
</li>
<li>
<p>The words that are not variable assignments or redirections shall be expanded. If any fields remain following their expansion,
the first field shall be considered the command name and remaining fields are the arguments for the command.</p>
</li>
<li>
<p>Redirections shall be performed as described in <a href="#tag_02_07">Redirection</a> .</p>
</li>
<li>
<p>Each variable assignment shall be expanded for tilde expansion, parameter expansion, command substitution, arithmetic expansion,
and quote removal prior to assigning the value.</p>
</li>
</ol>
<p>In the preceding list, the order of steps 3 and 4 may be reversed for the processing of special built-in utilities; see <a href=
"#tag_02_14">Special Built-In Utilities</a> .</p>
<p>If no command name results, variable assignments shall affect the current execution environment. Otherwise, the variable
assignments shall be exported for the execution environment of the command and shall not affect the current execution environment
(except for special built-ins). If any of the variable assignments attempt to assign a value to a read-only variable, a variable
assignment error shall occur. See <a href="#tag_02_08_01">Consequences of Shell Errors</a> for the consequences of these
errors.</p>
<p>If there is no command name, any redirections shall be performed in a subshell environment; it is unspecified whether this
subshell environment is the same one as that used for a command substitution within the command. (To affect the current execution
environment, see the <a href="exec.html#tag_04_46"><i>exec</i>()</a> special built-in.) If any of the redirections performed in the
current shell execution environment fail, the command shall immediately fail with an exit status greater than zero, and the shell
shall write an error message indicating the failure. See <a href="#tag_02_08_01">Consequences of Shell Errors</a> for the
consequences of these failures on interactive and non-interactive shells.</p>
<p>If there is a command name, execution shall continue as described in <a href="#tag_02_09_01_01">Command Search and Execution</a>
. If there is no command name, but the command contained a command substitution, the command shall complete with the exit status of
the last command substitution performed. Otherwise, the command shall complete with a zero exit status.</p>
<h5><a name="tag_02_09_01_01"></a>Command Search and Execution</h5>
<p>If a simple command results in a command name and an optional list of arguments, the following actions shall be performed:</p>
<ol>
<li>
<p>If the command name does not contain any slashes, the first successful step in the following sequence shall occur:</p>
<ol type="a">
<li>
<p>If the command name matches the name of a special built-in utility, that special built-in utility shall be invoked.</p>
</li>
<li>
<p>If the command name matches the name of a function known to this shell, the function shall be invoked as described in <a href=
"#tag_02_09_05">Function Definition Command</a> . If the implementation has provided a standard utility in the form of a function,
it shall not be recognized at this point. It shall be invoked in conjunction with the path search in step 1d.</p>
</li>
<li>
<p>If the command name matches the name of a utility listed in the following table, that utility shall be invoked.</p>
<center>
<table cellpadding="3" align="center">
<tr valign="top">
<td align="left">
<p class="tent"><br>
<a href="../utilities/alias.html"><i>alias</i></a><br>
<a href="../utilities/bg.html"><i>bg</i></a><br>
<a href="../utilities/cd.html"><i>cd</i></a><br>
<a href="../utilities/command.html"><i>command</i></a><br>
&nbsp;</p>
</td>
<td align="left">
<p class="tent"><br>
<a href="../utilities/false.html"><i>false</i></a><br>
<a href="../utilities/fc.html"><i>fc</i></a><br>
<a href="../utilities/fg.html"><i>fg</i></a><br>
<a href="../utilities/getopts.html"><i>getopts</i></a><br>
&nbsp;</p>
</td>
<td align="left">
<p class="tent"><br>
<a href="../utilities/jobs.html"><i>jobs</i></a><br>
<a href="../utilities/kill.html"><i>kill</i></a><br>
<a href="../utilities/newgrp.html"><i>newgrp</i></a><br>
<a href="../utilities/pwd.html"><i>pwd</i></a><br>
&nbsp;</p>
</td>
<td align="left">
<p class="tent"><br>
<a href="../utilities/read.html"><i>read</i></a><br>
<a href="../utilities/true.html"><i>true</i></a><br>
<a href="../utilities/umask.html"><i>umask</i></a><br>
<a href="../utilities/unalias.html"><i>unalias</i></a><br>
&nbsp;</p>
</td>
<td align="left">
<p class="tent"><br>
<a href="../utilities/wait.html"><i>wait</i></a><br>
&nbsp;</p>
</td>
</tr>
</table>
</center>
</li>
<li>
<p>Otherwise, the command shall be searched for using the <i>PATH</i> environment variable as described in the Base Definitions
volume of IEEE&nbsp;Std&nbsp;1003.1-2001, <a href="../basedefs/xbd_chap08.html">Chapter 8, Environment Variables</a>:</p>
<ol type="i">
<li>
<p>If the search is successful:</p>
<ol type="a">
<li>
<p>If the system has implemented the utility as a regular built-in or as a shell function, it shall be invoked at this point in the
path search.</p>
</li>
<li>
<p>Otherwise, the shell executes the utility in a separate utility environment (see <a href="#tag_02_12">Shell Execution
Environment</a> ) with actions equivalent to calling the <a href="../functions/execve.html"><i>execve</i>()</a> function as defined
in the System Interfaces volume of IEEE&nbsp;Std&nbsp;1003.1-2001 with the <i>path</i> argument set to the pathname resulting from
the search, <i>arg</i>0 set to the command name, and the remaining arguments set to the operands, if any.</p>
<p>If the <a href="../functions/execve.html"><i>execve</i>()</a> function fails due to an error equivalent to the [ENOEXEC] error
defined in the System Interfaces volume of IEEE&nbsp;Std&nbsp;1003.1-2001, the shell shall execute a command equivalent to having a
shell invoked with the command name as its first operand, with any remaining arguments passed to the new shell. If the executable
file is not a text file, the shell may bypass this command execution. In this case, it shall write an error message, and shall
return an exit status of 126.</p>
</li>
</ol>
<p>Once a utility has been searched for and found (either as a result of this specific search or as part of an unspecified shell
start-up activity), an implementation may remember its location and need not search for the utility again unless the <i>PATH</i>
variable has been the subject of an assignment. If the remembered location fails for a subsequent invocation, the shell shall
repeat the search to find the new location for the utility, if any.</p>
</li>
<li>
<p>If the search is unsuccessful, the command shall fail with an exit status of 127 and the shell shall write an error message.</p>
</li>
</ol>
</li>
</ol>
</li>
<li>
<p>If the command name contains at least one slash, the shell shall execute the utility in a separate utility environment with
actions equivalent to calling the <a href="../functions/execve.html"><i>execve</i>()</a> function defined in the System Interfaces
volume of IEEE&nbsp;Std&nbsp;1003.1-2001 with the <i>path</i> and <i>arg</i>0 arguments set to the command name, and the remaining
arguments set to the operands, if any.</p>
<p>If the <a href="../functions/execve.html"><i>execve</i>()</a> function fails due to an error equivalent to the [ENOEXEC] error,
the shell shall execute a command equivalent to having a shell invoked with the command name as its first operand, with any
remaining arguments passed to the new shell. If the executable file is not a text file, the shell may bypass this command
execution. In this case, it shall write an error message and shall return an exit status of 126.</p>
</li>
</ol>
<h4><a name="tag_02_09_02"></a>Pipelines</h4>
<p>A <i>pipeline</i> is a sequence of one or more commands separated by the control operator <tt>'|'</tt> . The standard output of
all but the last command shall be connected to the standard input of the next command.</p>
<p>The format for a pipeline is:</p>
<blockquote>
<pre>
<b>[</b><tt>!</tt><b>]</b> <i>command1</i> <b>[</b> <tt>|</tt> <i>command2</i> <tt>...</tt><b>]</b>
</pre>
</blockquote>
<p>The standard output of <i>command1</i> shall be connected to the standard input of <i>command2</i>. The standard input, standard
output, or both of a command shall be considered to be assigned by the pipeline before any redirection specified by redirection
operators that are part of the command (see <a href="#tag_02_07">Redirection</a> ).</p>
<p>If the pipeline is not in the background (see <a href="#tag_02_09_03_02">Asynchronous Lists</a> ), the shell shall wait for the
last command specified in the pipeline to complete, and may also wait for all commands to complete.</p>
<h5><a name="tag_02_09_02_01"></a>Exit Status</h5>
<p>If the reserved word <b>!</b> does not precede the pipeline, the exit status shall be the exit status of the last command
specified in the pipeline. Otherwise, the exit status shall be the logical NOT of the exit status of the last command. That is, if
the last command returns zero, the exit status shall be 1; if the last command returns greater than zero, the exit status shall be
zero.</p>
<h4><a name="tag_02_09_03"></a>Lists</h4>
<p>An <i>AND-OR list</i> is a sequence of one or more pipelines separated by the operators <tt>"&amp;&amp;"</tt> and <tt>"||"</tt>
.</p>
<p>A <i>list</i> is a sequence of one or more AND-OR lists separated by the operators <tt>';'</tt> and <tt>'&amp;'</tt> and
optionally terminated by <tt>';'</tt> , <tt>'&amp;'</tt> , or &lt;newline&gt;.</p>
<p>The operators <tt>"&amp;&amp;"</tt> and <tt>"||"</tt> shall have equal precedence and shall be evaluated with left
associativity. For example, both of the following commands write solely <b>bar</b> to standard output:</p>
<blockquote>
<pre>
<tt>false &amp;&amp; echo foo || echo bar
true || echo foo &amp;&amp; echo bar
</tt>
</pre>
</blockquote>
<p>A <tt>';'</tt> or &lt;newline&gt; terminator shall cause the preceding AND-OR list to be executed sequentially; an
<tt>'&amp;'</tt> shall cause asynchronous execution of the preceding AND-OR list.</p>
<p>The term &quot;compound-list&quot; is derived from the grammar in <a href="#tag_02_10">Shell Grammar</a> ; it is equivalent to a
sequence of <i>lists</i>, separated by &lt;newline&gt;s, that can be preceded or followed by an arbitrary number of
&lt;newline&gt;s.</p>
<hr>
<div class="box"><em>The following sections are informative.</em></div>
<h5><a name="tag_02_09_03_01"></a>Examples</h5>
<p>The following is an example that illustrates &lt;newline&gt;s in compound-lists:</p>
<blockquote>
<pre>
<tt>while
# a couple of &lt;newline&gt;s
<br>
# a list
date &amp;&amp; who || ls; cat file
# a couple of &lt;newline&gt;s
<br>
# another list
wc file &gt; output &amp; true
<br>
do
# 2 lists
ls
cat file
done
</tt>
</pre>
</blockquote>
<div class="box"><em>End of informative text.</em></div>
<hr>
<h5><a name="tag_02_09_03_02"></a>Asynchronous Lists</h5>
<p>If a command is terminated by the control operator ampersand ( <tt>'&amp;'</tt> ), the shell shall execute the command
asynchronously in a subshell. This means that the shell shall not wait for the command to finish before executing the next
command.</p>
<p>The format for running a command in the background is:</p>
<blockquote>
<pre>
<i>command1</i> <tt>&amp;</tt> <b>[</b><i>command2</i> <tt>&amp; ...</tt> <b>]</b>
</pre>
</blockquote>
<p>The standard input for an asynchronous list, before any explicit redirections are performed, shall be considered to be assigned
to a file that has the same properties as <b>/dev/null</b>. If it is an interactive shell, this need not happen. In all cases,
explicit redirection of standard input shall override this activity.</p>
<p>When an element of an asynchronous list (the portion of the list ended by an ampersand, such as <i>command1</i>, above) is
started by the shell, the process ID of the last command in the asynchronous list element shall become known in the current shell
execution environment; see <a href="#tag_02_12">Shell Execution Environment</a> . This process ID shall remain known until:</p>
<ol>
<li>
<p>The command terminates and the application waits for the process ID.</p>
</li>
<li>
<p>Another asynchronous list invoked before <tt>"$!"</tt> (corresponding to the previous asynchronous list) is expanded in the
current execution environment.</p>
</li>
</ol>
<p>The implementation need not retain more than the {CHILD_MAX} most recent entries in its list of known process IDs in the current
shell execution environment.</p>
<h5><a name="tag_02_09_03_03"></a>Exit Status</h5>
<p>The exit status of an asynchronous list shall be zero.</p>
<h5><a name="tag_02_09_03_04"></a>Sequential Lists</h5>
<p>Commands that are separated by a semicolon ( <tt>';'</tt> ) shall be executed sequentially.</p>
<p>The format for executing commands sequentially shall be:</p>
<blockquote>
<pre>
<i>command1</i> <b>[</b><tt>;</tt> <i>command2</i><b>]</b> <tt>...
</tt>
</pre>
</blockquote>
<p>Each command shall be expanded and executed in the order specified.</p>
<h5><a name="tag_02_09_03_05"></a>Exit Status</h5>
<p>The exit status of a sequential list shall be the exit status of the last command in the list.</p>
<h5><a name="tag_02_09_03_06"></a>AND Lists</h5>
<p>The control operator <tt>"&amp;&amp;"</tt> denotes an AND list. The format shall be:</p>
<blockquote>
<pre>
<i>command1</i> <b>[</b> <tt>&amp;&amp;</tt> <i>command2</i><b>]</b> <tt>...
</tt>
</pre>
</blockquote>
<p>First <i>command1</i> shall be executed. If its exit status is zero, <i>command2</i> shall be executed, and so on, until a
command has a non-zero exit status or there are no more commands left to execute. The commands are expanded only if they are
executed.</p>
<h5><a name="tag_02_09_03_07"></a>Exit Status</h5>
<p>The exit status of an AND list shall be the exit status of the last command that is executed in the list.</p>
<h5><a name="tag_02_09_03_08"></a>OR Lists</h5>
<p>The control operator <tt>"||"</tt> denotes an OR List. The format shall be:</p>
<blockquote>
<pre>
<i>command1</i> <b>[</b> <tt>||</tt> <i>command2</i><b>]</b> <tt>...
</tt>
</pre>
</blockquote>
<p>First, <i>command1</i> shall be executed. If its exit status is non-zero, <i>command2</i> shall be executed, and so on, until a
command has a zero exit status or there are no more commands left to execute.</p>
<h5><a name="tag_02_09_03_09"></a>Exit Status</h5>
<p>The exit status of an OR list shall be the exit status of the last command that is executed in the list.</p>
<h4><a name="tag_02_09_04"></a>Compound Commands</h4>
<p>The shell has several programming constructs that are &quot;compound commands&quot;, which provide control flow for commands. Each of
these compound commands has a reserved word or control operator at the beginning, and a corresponding terminator reserved word or
operator at the end. In addition, each can be followed by redirections on the same line as the terminator. Each redirection shall
apply to all the commands within the compound command that do not explicitly override that redirection.</p>
<h5><a name="tag_02_09_04_01"></a>Grouping Commands</h5>
<p>The format for grouping commands is as follows:</p>
<dl compact>
<dt>(<i>compound-list</i>)</dt>
<dd>Execute <i>compound-list</i> in a subshell environment; see <a href="#tag_02_12">Shell Execution Environment</a> . Variable
assignments and built-in commands that affect the environment shall not remain in effect after the list finishes.</dd>
<dt>{&nbsp;<i>compound-list</i>;}</dt>
<dd>Execute <i>compound-list</i> in the current process environment. The semicolon shown here is an example of a control operator
delimiting the <b>}</b> reserved word. Other delimiters are possible, as shown in <a href="#tag_02_10">Shell Grammar</a> ; a
&lt;newline&gt; is frequently used.</dd>
</dl>
<h5><a name="tag_02_09_04_02"></a>Exit Status</h5>
<p>The exit status of a grouping command shall be the exit status of <i>compound-list</i>.</p>
<h5><a name="tag_02_09_04_03"></a>The for Loop</h5>
<p>The <b>for</b> loop shall execute a sequence of commands for each member in a list of <i>items</i>. The <b>for</b> loop requires
that the reserved words <b>do</b> and <b>done</b> be used to delimit the sequence of commands.</p>
<p>The format for the <b>for</b> loop is as follows:</p>
<blockquote>
<pre>
<tt>for</tt> <i>name</i> <tt></tt><b>[</b> <tt>in</tt> <b>[</b><i>word</i> <tt>...</tt> <b>]]</b><tt>do
</tt> <i>compound-list</i><tt>done
</tt>
</pre>
</blockquote>
<p>First, the list of words following <b>in</b> shall be expanded to generate a list of items. Then, the variable <i>name</i> shall
be set to each item, in turn, and the <i>compound-list</i> executed each time. If no items result from the expansion, the
<i>compound-list</i> shall not be executed. Omitting:</p>
<blockquote>
<pre>
<tt>in</tt> <i>word</i> ...
</pre>
</blockquote>
<p>shall be equivalent to:</p>
<blockquote>
<pre>
<tt>in "$@"
</tt>
</pre>
</blockquote>
<h5><a name="tag_02_09_04_04"></a>Exit Status</h5>
<p>The exit status of a <b>for</b> command shall be the exit status of the last command that executes. If there are no items, the
exit status shall be zero.</p>
<h5><a name="tag_02_09_04_05"></a>Case Conditional Construct</h5>
<p>The conditional construct <b>case</b> shall execute the <i>compound-list</i> corresponding to the first one of several
<i>patterns</i> (see <a href="#tag_02_13">Pattern Matching Notation</a> ) that is matched by the string resulting from the tilde
expansion, parameter expansion, command substitution, arithmetic expansion, and quote removal of the given word. The reserved word
<b>in</b> shall denote the beginning of the patterns to be matched. Multiple patterns with the same <i>compound-list</i> shall be
delimited by the <tt>'|'</tt> symbol. The control operator <tt>')'</tt> terminates a list of patterns corresponding to a given
action. The <i>compound-list</i> for each list of patterns, with the possible exception of the last, shall be terminated with
<tt>";;"</tt> . The <b>case</b> construct terminates with the reserved word <b>esac</b> ( <b>case</b> reversed).</p>
<p>The format for the <b>case</b> construct is as follows:</p>
<blockquote>
<pre>
<tt>case</tt> <i>word</i> <tt>in
</tt> <b>[</b><tt>(</tt><b>]</b><i>pattern1</i><tt>)</tt> <i>compound-list</i><tt>;;
</tt> <b>[[</b><tt>(</tt><b>]</b><i>pattern</i><b>[</b> <tt>|</tt> <i>pattern</i><b>]</b> <tt>... )</tt> <i>compound-list</i><tt>;;</tt><b>]</b> <tt>...
</tt> <b>[[</b><tt>(</tt><b>]</b><i>pattern</i><b>[</b> <tt>|</tt> <i>pattern</i><b>]</b> <tt>... )</tt> <i>compound-list</i><b>]</b><tt>esac
</tt>
</pre>
</blockquote>
<p>The <tt>";;"</tt> is optional for the last <i>compound-list</i>.</p>
<p>In order from the beginning to the end of the <b>case</b> statement, each <i>pattern</i> that labels a <i>compound-list</i>
shall be subjected to tilde expansion, parameter expansion, command substitution, and arithmetic expansion, and the result of these
expansions shall be compared against the expansion of <i>word</i>, according to the rules described in <a href="#tag_02_13">Pattern
Matching Notation</a> (which also describes the effect of quoting parts of the pattern). After the first match, no more patterns
shall be expanded, and the <i>compound-list</i> shall be executed. The order of expansion and comparison of multiple
<i>pattern</i>s that label a <i>compound-list</i> statement is unspecified.</p>
<h5><a name="tag_02_09_04_06"></a>Exit Status</h5>
<p>The exit status of <b>case</b> shall be zero if no patterns are matched. Otherwise, the exit status shall be the exit status of
the last command executed in the <i>compound-list</i>.</p>
<h5><a name="tag_02_09_04_07"></a>The if Conditional Construct</h5>
<p>The <b>if</b> command shall execute a <i>compound-list</i> and use its exit status to determine whether to execute another
<i>compound-list</i>.</p>
<p>The format for the <b>if</b> construct is as follows:</p>
<blockquote>
<pre>
<tt>if</tt> <i>compound-list</i><tt>then
</tt> <i>compound-list</i><b>[</b><tt>elif</tt> <i>compound-list</i><tt>then
</tt> <i>compound-list</i><b>]</b> <tt>...
</tt><b>[</b><tt>else
</tt> <i>compound-list</i><b>]</b><tt>fi
</tt>
</pre>
</blockquote>
<p>The <b>if</b> <i>compound-list</i> shall be executed; if its exit status is zero, the <b>then</b> <i>compound-list</i> shall be
executed and the command shall complete. Otherwise, each <b>elif</b> <i>compound-list</i> shall be executed, in turn, and if its
exit status is zero, the <b>then</b> <i>compound-list</i> shall be executed and the command shall complete. Otherwise, the
<b>else</b> <i>compound-list</i> shall be executed.</p>
<h5><a name="tag_02_09_04_08"></a>Exit Status</h5>
<p>The exit status of the <b>if</b> command shall be the exit status of the <b>then</b> or <b>else</b> <i>compound-list</i> that
was executed, or zero, if none was executed.</p>
<h5><a name="tag_02_09_04_09"></a>The while Loop</h5>
<p>The <b>while</b> loop shall continuously execute one <i>compound-list</i> as long as another <i>compound-list</i> has a zero
exit status.</p>
<p>The format of the <b>while</b> loop is as follows:</p>
<blockquote>
<pre>
<tt>while</tt> <i>compound-list-1</i><tt>do
</tt> <i>compound-list-2</i><tt>done
</tt>
</pre>
</blockquote>
<p>The <i>compound-list-1</i> shall be executed, and if it has a non-zero exit status, the <b>while</b> command shall complete.
Otherwise, the <i>compound-list-2</i> shall be executed, and the process shall repeat.</p>
<h5><a name="tag_02_09_04_10"></a>Exit Status</h5>
<p>The exit status of the <b>while</b> loop shall be the exit status of the last <i>compound-list-2</i> executed, or zero if none
was executed.</p>
<h5><a name="tag_02_09_04_11"></a>The until Loop</h5>
<p>The <b>until</b> loop shall continuously execute one <i>compound-list</i> as long as another <i>compound-list</i> has a non-zero
exit status.</p>
<p>The format of the <b>until</b> loop is as follows:</p>
<blockquote>
<pre>
<tt>until</tt> <i>compound-list-1</i><tt>do
</tt> <i>compound-list-2</i><tt>done
</tt>
</pre>
</blockquote>
<p>The <i>compound-list-1</i> shall be executed, and if it has a zero exit status, the <b>until</b> command completes. Otherwise,
the <i>compound-list-2</i> shall be executed, and the process repeats.</p>
<h5><a name="tag_02_09_04_12"></a>Exit Status</h5>
<p>The exit status of the <b>until</b> loop shall be the exit status of the last <i>compound-list-2</i> executed, or zero if none
was executed.</p>
<h4><a name="tag_02_09_05"></a>Function Definition Command</h4>
<p>A function is a user-defined name that is used as a simple command to call a compound command with new positional parameters. A
function is defined with a &quot;function definition command&quot;.</p>
<p>The format of a function definition command is as follows:</p>
<blockquote>
<pre>
<i>fname</i><tt>()</tt> <i>compound-command</i><b>[</b><i>io-redirect</i> <tt>...</tt><b>]</b>
</pre>
</blockquote>
<p>The function is named <i>fname</i>; the application shall ensure that it is a name (see the Base Definitions volume of
IEEE&nbsp;Std&nbsp;1003.1-2001, <a href="../basedefs/xbd_chap03.html#tag_03_230">Section 3.230, Name</a>). An implementation may
allow other characters in a function name as an extension. The implementation shall maintain separate name spaces for functions and
variables.</p>
<p>The argument <i>compound-command</i> represents a compound command, as described in <a href="#tag_02_09_04">Compound
Commands</a> .</p>
<p>When the function is declared, none of the expansions in <a href="#tag_02_06">Word Expansions</a> shall be performed on the text
in <i>compound-command</i> or <i>io-redirect</i>; all expansions shall be performed as normal each time the function is called.
Similarly, the optional <i>io-redirect</i> redirections and any variable assignments within <i>compound-command</i> shall be
performed during the execution of the function itself, not the function definition. See <a href="#tag_02_08_01">Consequences of
Shell Errors</a> for the consequences of failures of these operations on interactive and non-interactive shells.</p>
<p>When a function is executed, it shall have the syntax-error and variable-assignment properties described for special built-in
utilities in the enumerated list at the beginning of <a href="#tag_02_14">Special Built-In Utilities</a> .</p>
<p>The <i>compound-command</i> shall be executed whenever the function name is specified as the name of a simple command (see <a
href="#tag_02_09_01_01">Command Search and Execution</a> ). The operands to the command temporarily shall become the positional
parameters during the execution of the <i>compound-command</i>; the special parameter <tt>'#'</tt> also shall be changed to reflect
the number of operands. The special parameter 0 shall be unchanged. When the function completes, the values of the positional
parameters and the special parameter <tt>'#'</tt> shall be restored to the values they had before the function was executed. If the
special built-in <a href="return.html"><i>return</i></a> is executed in the <i>compound-command</i>, the function completes and
execution shall resume with the next command after the function call.</p>
<h5><a name="tag_02_09_05_01"></a>Exit Status</h5>
<p>The exit status of a function definition shall be zero if the function was declared successfully; otherwise, it shall be greater
than zero. The exit status of a function invocation shall be the exit status of the last command executed by the function.</p>
<h3><a name="tag_02_10"></a>Shell Grammar</h3>
<p>The following grammar defines the Shell Command Language. This formal syntax shall take precedence over the preceding text
syntax description.</p>
<h4><a name="tag_02_10_01"></a>Shell Grammar Lexical Conventions</h4>
<p>The input language to the shell must be first recognized at the character level. The resulting tokens shall be classified by
their immediate context according to the following rules (applied in order). These rules shall be used to determine what a
&quot;token&quot; is that is subject to parsing at the token level. The rules for token recognition in <a href="#tag_02_03">Token
Recognition</a> shall apply.</p>
<ol>
<li>
<p>A &lt;newline&gt; shall be returned as the token identifier <b>NEWLINE</b>.</p>
</li>
<li>
<p>If the token is an operator, the token identifier for that operator shall result.</p>
</li>
<li>
<p>If the string consists solely of digits and the delimiter character is one of <tt>'&lt;'</tt> or <tt>'&gt;'</tt> , the token
identifier <b>IO_NUMBER</b> shall be returned.</p>
</li>
<li>
<p>Otherwise, the token identifier <b>TOKEN</b> results.</p>
</li>
</ol>
<p>Further distinction on <b>TOKEN</b> is context-dependent. It may be that the same <b>TOKEN</b> yields <b>WORD</b>, a
<b>NAME</b>, an <b>ASSIGNMENT</b>, or one of the reserved words below, dependent upon the context. Some of the productions in the
grammar below are annotated with a rule number from the following list. When a <b>TOKEN</b> is seen where one of those annotated
productions could be used to reduce the symbol, the applicable rule shall be applied to convert the token identifier type of the
<b>TOKEN</b> to a token identifier acceptable at that point in the grammar. The reduction shall then proceed based upon the token
identifier type yielded by the rule applied. When more than one rule applies, the highest numbered rule shall apply (which in turn
may refer to another rule). (Note that except in rule 7, the presence of an <tt>'='</tt> in the token has no effect.)</p>
<p>The <b>WORD</b> tokens shall have the word expansion rules applied to them immediately before the associated command is
executed, not at the time the command is parsed.</p>
<h4><a name="tag_02_10_02"></a>Shell Grammar Rules</h4>
<ol>
<li>
<p>[Command Name]</p>
<p>When the <b>TOKEN</b> is exactly a reserved word, the token identifier for that reserved word shall result. Otherwise, the token
<b>WORD</b> shall be returned. Also, if the parser is in any state where only a reserved word could be the next correct token,
proceed as above. <basefont size="2"></p>
<dl>
<dt><b>Note:</b></dt>
<dd>Because at this point quote marks are retained in the token, quoted strings cannot be recognized as reserved words. This rule
also implies that reserved words are not recognized except in certain positions in the input, such as after a &lt;newline&gt; or
semicolon; the grammar presumes that if the reserved word is intended, it is properly delimited by the user, and does not attempt
to reflect that requirement directly. Also note that line joining is done before tokenization, as described in <a href=
"#tag_02_02_01">Escape Character (Backslash)</a> , so escaped &lt;newline&gt;s are already removed at this point.</dd>
</dl>
<basefont size="3">
<p>Rule 1 is not directly referenced in the grammar, but is referred to by other rules, or applies globally.</p>
</li>
<li>
<p>[Redirection to or from filename]</p>
<p>The expansions specified in <a href="#tag_02_07">Redirection</a> shall occur. As specified there, exactly one field can result
(or the result is unspecified), and there are additional requirements on pathname expansion.</p>
</li>
<li>
<p>[Redirection from here-document]</p>
<p>Quote removal shall be applied to the word to determine the delimiter that is used to find the end of the here-document that
begins after the next &lt;newline&gt;.</p>
</li>
<li>
<p>[Case statement termination]</p>
<p>When the <b>TOKEN</b> is exactly the reserved word <b>esac</b>, the token identifier for <b>esac</b> shall result. Otherwise,
the token <b>WORD</b> shall be returned.</p>
</li>
<li>
<p>[ <b>NAME</b> in <b>for</b>]</p>
<p>When the <b>TOKEN</b> meets the requirements for a name (see the Base Definitions volume of IEEE&nbsp;Std&nbsp;1003.1-2001, <a
href="../basedefs/xbd_chap03.html#tag_03_230">Section 3.230, Name</a>), the token identifier <b>NAME</b> shall result. Otherwise,
the token <b>WORD</b> shall be returned.</p>
</li>
<li>
<p>[Third word of <b>for</b> and <b>case</b>]</p>
<ol type="a">
<li>
<p>[ <b>case</b> only]</p>
<p>When the <b>TOKEN</b> is exactly the reserved word <b>in</b>, the token identifier for <b>in</b> shall result. Otherwise, the
token <b>WORD</b> shall be returned.</p>
</li>
<li>
<p>[ <b>for</b> only]</p>
<p>When the <b>TOKEN</b> is exactly the reserved word <b>in</b> or <b>do</b>, the token identifier for <b>in</b> or <b>do</b> shall
result, respectively. Otherwise, the token <b>WORD</b> shall be returned.</p>
</li>
</ol>
<p>(For a. and b.: As indicated in the grammar, a <i>linebreak</i> precedes the tokens <b>in</b> and <b>do</b>. If &lt;newline&gt;s
are present at the indicated location, it is the token after them that is treated in this fashion.)</p>
</li>
<li>
<p>[Assignment preceding command name]</p>
<ol type="a">
<li>
<p>[When the first word]</p>
<p>If the <b>TOKEN</b> does not contain the character <tt>'='</tt> , rule 1 is applied. Otherwise, 7b shall be applied.</p>
</li>
<li>
<p>[Not the first word]</p>
<p>If the <b>TOKEN</b> contains the equal sign character:</p>
<ul>
<li>
<p>If it begins with <tt>'='</tt> , the token <b>WORD</b> shall be returned.</p>
</li>
<li>
<p>If all the characters preceding <tt>'='</tt> form a valid name (see the Base Definitions volume of
IEEE&nbsp;Std&nbsp;1003.1-2001, <a href="../basedefs/xbd_chap03.html#tag_03_230">Section 3.230, Name</a>), the token
<b>ASSIGNMENT_WORD</b> shall be returned. (Quoted characters cannot participate in forming a valid name.)</p>
</li>
<li>
<p>Otherwise, it is unspecified whether it is <b>ASSIGNMENT_WORD</b> or <b>WORD</b> that is returned.</p>
</li>
</ul>
</li>
</ol>
<p>Assignment to the <b>NAME</b> shall occur as specified in <a href="#tag_02_09_01">Simple Commands</a> .</p>
</li>
<li>
<p>[ <b>NAME</b> in function]</p>
<p>When the <b>TOKEN</b> is exactly a reserved word, the token identifier for that reserved word shall result. Otherwise, when the
<b>TOKEN</b> meets the requirements for a name, the token identifier <b>NAME</b> shall result. Otherwise, rule 7 applies.</p>
</li>
<li>
<p>[Body of function]</p>
<p>Word expansion and assignment shall never occur, even when required by the rules above, when this rule is being parsed. Each
<b>TOKEN</b> that might either be expanded or have assignment applied to it shall instead be returned as a single <b>WORD</b>
consisting only of characters that are exactly the token described in <a href="#tag_02_03">Token Recognition</a> .</p>
</li>
</ol>
<br>
<pre>
<tt>/* -------------------------------------------------------
The grammar symbols
------------------------------------------------------- */
<br>
%token WORD
%token ASSIGNMENT_WORD
%token NAME
%token NEWLINE
%token IO_NUMBER
<br>
/* The following are the operators mentioned above. */
<br>
%token AND_IF OR_IF DSEMI
/* '&amp;&amp;' '||' ';;' */
<br>
%token DLESS DGREAT LESSAND GREATAND LESSGREAT DLESSDASH
/* '&lt;&lt;' '&gt;&gt;' '&lt;&amp;' '&gt;&amp;' '&lt;&gt;' '&lt;&lt;-' */
<br>
%token CLOBBER
/* '&gt;|' */
<br>
/* The following are the reserved words. */
<br>
%token If Then Else Elif Fi Do Done
/* 'if' 'then' 'else' 'elif' 'fi' 'do' 'done' */
<br>
%token Case Esac While Until For
/* 'case' 'esac' 'while' 'until' 'for' */
<br>
/* These are reserved words, not operator tokens, and are
recognized when reserved words are recognized. */
<br>
%token Lbrace Rbrace Bang
/* '{' '}' '!' */
<br>
%token In
/* 'in' */
<br>
/* -------------------------------------------------------
The Grammar
------------------------------------------------------- */
<br>
%start complete_command
%%
complete_command : list separator
| list
;
list : list separator_op and_or
| and_or
;
and_or : pipeline
| and_or AND_IF linebreak pipeline
| and_or OR_IF linebreak pipeline
;
pipeline : pipe_sequence
| Bang pipe_sequence
;
pipe_sequence : command
| pipe_sequence '|' linebreak command
;
command : simple_command
| compound_command
| compound_command redirect_list
| function_definition
;
compound_command : brace_group
| subshell
| for_clause
| case_clause
| if_clause
| while_clause
| until_clause
;
subshell : '(' compound_list ')'
;
compound_list : term
| newline_list term
| term separator
| newline_list term separator
;
term : term separator and_or
| and_or
;
for_clause : For name linebreak do_group
| For name linebreak in sequential_sep do_group
| For name linebreak in wordlist sequential_sep do_group
;
name : NAME /* Apply rule 5 */
;
in : In /* Apply rule 6 */
;
wordlist : wordlist WORD
| WORD
;
case_clause : Case WORD linebreak in linebreak case_list Esac
| Case WORD linebreak in linebreak case_list_ns Esac
| Case WORD linebreak in linebreak Esac
;
case_list_ns : case_list case_item_ns
| case_item_ns
;
case_list : case_list case_item
| case_item
;
case_item_ns : pattern ')' linebreak
| pattern ')' compound_list linebreak
| '(' pattern ')' linebreak
| '(' pattern ')' compound_list linebreak
;
case_item : pattern ')' linebreak DSEMI linebreak
| pattern ')' compound_list DSEMI linebreak
| '(' pattern ')' linebreak DSEMI linebreak
| '(' pattern ')' compound_list DSEMI linebreak
;
pattern : WORD /* Apply rule 4 */
| pattern '|' WORD /* Do not apply rule 4 */
;
if_clause : If compound_list Then compound_list else_part Fi
| If compound_list Then compound_list Fi
;
else_part : Elif compound_list Then else_part
| Else compound_list
;
while_clause : While compound_list do_group
;
until_clause : Until compound_list do_group
;
function_definition : fname '(' ')' linebreak function_body
;
function_body : compound_command /* Apply rule 9 */
| compound_command redirect_list /* Apply rule 9 */
;
fname : NAME /* Apply rule 8 */
;
brace_group : Lbrace compound_list Rbrace
;
do_group : Do compound_list Done /* Apply rule 6 */
;
simple_command : cmd_prefix cmd_word cmd_suffix
| cmd_prefix cmd_word
| cmd_prefix
| cmd_name cmd_suffix
| cmd_name
;
cmd_name : WORD /* Apply rule 7a */
;
cmd_word : WORD /* Apply rule 7b */
;
cmd_prefix : io_redirect
| cmd_prefix io_redirect
| ASSIGNMENT_WORD
| cmd_prefix ASSIGNMENT_WORD
;
cmd_suffix : io_redirect
| cmd_suffix io_redirect
| WORD
| cmd_suffix WORD
;
redirect_list : io_redirect
| redirect_list io_redirect
;
io_redirect : io_file
| IO_NUMBER io_file
| io_here
| IO_NUMBER io_here
;
io_file : '&lt;' filename
| LESSAND filename
| '&gt;' filename
| GREATAND filename
| DGREAT filename
| LESSGREAT filename
| CLOBBER filename
;
filename : WORD /* Apply rule 2 */
;
io_here : DLESS here_end
| DLESSDASH here_end
;
here_end : WORD /* Apply rule 3 */
;
newline_list : NEWLINE
| newline_list NEWLINE
;
linebreak : newline_list
| /* empty */
;
separator_op : '&amp;'
| ';'
;
separator : separator_op linebreak
| newline_list
;
sequential_sep : ';' linebreak
| newline_list
;
</tt>
</pre>
<h3><a name="tag_02_11"></a>Signals and Error Handling</h3>
<p>When a command is in an asynchronous list, the shell shall prevent SIGQUIT and SIGINT signals from the keyboard from
interrupting the command. Otherwise, signals shall have the values inherited by the shell from its parent (see also the <a href=
"trap.html#tag_04_146"><i>trap</i></a> special built-in).</p>
<p>When a signal for which a trap has been set is received while the shell is waiting for the completion of a utility executing a
foreground command, the trap associated with that signal shall not be executed until after the foreground command has completed.
When the shell is waiting, by means of the <a href="../utilities/wait.html"><i>wait</i></a> utility, for asynchronous commands to
complete, the reception of a signal for which a trap has been set shall cause the <a href="../utilities/wait.html"><i>wait</i></a>
utility to return immediately with an exit status &gt;128, immediately after which the trap associated with that signal shall be
taken.</p>
<p>If multiple signals are pending for the shell for which there are associated trap actions, the order of execution of trap
actions is unspecified.</p>
<h3><a name="tag_02_12"></a>Shell Execution Environment</h3>
<p>A shell execution environment consists of the following:</p>
<ul>
<li>
<p>Open files inherited upon invocation of the shell, plus open files controlled by <a href="exec.html"><i>exec</i></a></p>
</li>
<li>
<p>Working directory as set by <a href="../utilities/cd.html"><i>cd</i></a></p>
</li>
<li>
<p>File creation mask set by <a href="../utilities/umask.html"><i>umask</i></a></p>
</li>
<li>
<p>Current traps set by <a href="trap.html"><i>trap</i></a></p>
</li>
<li>
<p>Shell parameters that are set by variable assignment (see the <a href="set.html#tag_04_127"><i>set</i></a> special built-in) or
from the System Interfaces volume of IEEE&nbsp;Std&nbsp;1003.1-2001 environment inherited by the shell when it begins (see the <a
href="export.html#tag_04_49"><i>export</i></a> special built-in)</p>
</li>
<li>
<p>Shell functions; see <a href="#tag_02_09_05">Function Definition Command</a></p>
</li>
<li>
<p>Options turned on at invocation or by <a href="set.html"><i>set</i></a></p>
</li>
<li>
<p>Process IDs of the last commands in asynchronous lists known to this shell environment; see <a href=
"#tag_02_09_03_02">Asynchronous Lists</a></p>
</li>
<li>
<p>Shell aliases; see <a href="#tag_02_03_01">Alias Substitution</a></p>
</li>
</ul>
<p>Utilities other than the special built-ins (see <a href="#tag_02_14">Special Built-In Utilities</a> ) shall be invoked in a
separate environment that consists of the following. The initial value of these objects shall be the same as that for the parent
shell, except as noted below.</p>
<ul>
<li>
<p>Open files inherited on invocation of the shell, open files controlled by the <a href="exec.html"><i>exec</i></a> special built-in
plus any modifications, and additions specified by any redirections to the utility</p>
</li>
<li>
<p>Current working directory</p>
</li>
<li>
<p>File creation mask</p>
</li>
<li>
<p>If the utility is a shell script, traps caught by the shell shall be set to the default values and traps ignored by the shell
shall be set to be ignored by the utility; if the utility is not a shell script, the trap actions (default or ignore) shall be
mapped into the appropriate signal handling actions for the utility</p>
</li>
<li>
<p>Variables with the <a href="export.html"><i>export</i></a> attribute, along with those explicitly exported for the duration of the
command, shall be passed to the utility environment variables</p>
</li>
</ul>
<p>The environment of the shell process shall not be changed by the utility unless explicitly specified by the utility description
(for example, <a href="../utilities/cd.html"><i>cd</i></a> and <a href="../utilities/umask.html"><i>umask</i></a>).</p>
<p>A subshell environment shall be created as a duplicate of the shell environment, except that signal traps set by that shell
environment shall be set to the default values. Changes made to the subshell environment shall not affect the shell environment.
Command substitution, commands that are grouped with parentheses, and asynchronous lists shall be executed in a subshell
environment. Additionally, each command of a multi-command pipeline is in a subshell environment; as an extension, however, any or
all commands in a pipeline may be executed in the current environment. All other commands shall be executed in the current shell
environment.</p>
<h3><a name="tag_02_13"></a>Pattern Matching Notation</h3>
<p>The pattern matching notation described in this section is used to specify patterns for matching strings in the shell.
Historically, pattern matching notation is related to, but slightly different from, the regular expression notation described in
the Base Definitions volume of IEEE&nbsp;Std&nbsp;1003.1-2001, <a href="../basedefs/xbd_chap09.html">Chapter 9, Regular
Expressions</a>. For this reason, the description of the rules for this pattern matching notation are based on the description of
regular expression notation, modified to include backslash escape processing.</p>
<h4><a name="tag_02_13_01"></a>Patterns Matching a Single Character</h4>
<p>The following patterns matching a single character shall match a single character: ordinary characters, special pattern
characters, and pattern bracket expressions. The pattern bracket expression also shall match a single collating element. A
backslash character shall escape the following character. The escaping backslash shall be discarded.</p>
<p>An ordinary character is a pattern that shall match itself. It can be any character in the supported character set except for
NUL, those special shell characters in <a href="#tag_02_02">Quoting</a> that require quoting, and the following three special
pattern characters. Matching shall be based on the bit pattern used for encoding the character, not on the graphic representation
of the character. If any character (ordinary, shell special, or pattern special) is quoted, that pattern shall match the character
itself. The shell special characters always require quoting.</p>
<p>When unquoted and outside a bracket expression, the following three characters shall have special meaning in the specification
of patterns:</p>
<dl compact>
<dt><tt>?</tt></dt>
<dd>A question-mark is a pattern that shall match any character.</dd>
<dt><tt>*</tt></dt>
<dd>An asterisk is a pattern that shall match multiple characters, as described in <a href="#tag_02_13_02">Patterns Matching
Multiple Characters</a> .</dd>
<dt><tt>[</tt></dt>
<dd>The open bracket shall introduce a pattern bracket expression.</dd>
</dl>
<p>The description of basic regular expression bracket expressions in the Base Definitions volume of
IEEE&nbsp;Std&nbsp;1003.1-2001, <a href="../basedefs/xbd_chap09.html#tag_09_03_05">Section 9.3.5, RE Bracket Expression</a> shall
also apply to the pattern bracket expression, except that the exclamation mark character ( <tt>'!'</tt> ) shall replace the
circumflex character ( <tt>'^'</tt> ) in its role in a &quot;non-matching list&quot; in the regular expression notation. A bracket
expression starting with an unquoted circumflex character produces unspecified results.</p>
<p>When pattern matching is used where shell quote removal is not performed (such as in the argument to the <a href=
"../utilities/find.html"><i>find</i></a> - <i>name</i> primary when <a href="../utilities/find.html"><i>find</i></a> is being
called using one of the <i>exec</i> functions as defined in the System Interfaces volume of IEEE&nbsp;Std&nbsp;1003.1-2001, or in
the <i>pattern</i> argument to the <a href="../functions/fnmatch.html"><i>fnmatch</i>()</a> function), special characters can be
escaped to remove their special meaning by preceding them with a backslash character. This escaping backslash is discarded. The
sequence <tt>"\\"</tt> represents one literal backslash. All of the requirements and effects of quoting on ordinary, shell special,
and special pattern characters shall apply to escaping in this context.</p>
<h4><a name="tag_02_13_02"></a>Patterns Matching Multiple Characters</h4>
<p>The following rules are used to construct patterns matching multiple characters from patterns matching a single character:</p>
<ol>
<li>
<p>The asterisk ( <tt>'*'</tt> ) is a pattern that shall match any string, including the null string.</p>
</li>
<li>
<p>The concatenation of patterns matching a single character is a valid pattern that shall match the concatenation of the single
characters or collating elements matched by each of the concatenated patterns.</p>
</li>
<li>
<p>The concatenation of one or more patterns matching a single character with one or more asterisks is a valid pattern. In such
patterns, each asterisk shall match a string of zero or more characters, matching the greatest possible number of characters that
still allows the remainder of the pattern to match the string.</p>
</li>
</ol>
<h4><a name="tag_02_13_03"></a>Patterns Used for Filename Expansion</h4>
<p>The rules described so far in <a href="#tag_02_13_01">Patterns Matching a Single Character</a> and <a href=
"#tag_02_13_02">Patterns Matching Multiple Characters</a> are qualified by the following rules that apply when pattern matching
notation is used for filename expansion:</p>
<ol>
<li>
<p>The slash character in a pathname shall be explicitly matched by using one or more slashes in the pattern; it shall neither be
matched by the asterisk or question-mark special characters nor by a bracket expression. Slashes in the pattern shall be identified
before bracket expressions; thus, a slash cannot be included in a pattern bracket expression used for filename expansion. If a
slash character is found following an unescaped open square bracket character before a corresponding closing square bracket is
found, the open bracket shall be treated as an ordinary character. For example, the pattern <tt>"a[b/c]d"</tt> does not match such
pathnames as <b>abd</b> or <b>a/d</b>. It only matches a pathname of literally <b>a[b/c]d</b>.</p>
</li>
<li>
<p>If a filename begins with a period ( <tt>'.'</tt> ), the period shall be explicitly matched by using a period as the first
character of the pattern or immediately following a slash character. The leading period shall not be matched by:</p>
<ul>
<li>
<p>The asterisk or question-mark special characters</p>
</li>
<li>
<p>A bracket expression containing a non-matching list, such as <tt>"[!a]"</tt> , a range expression, such as <tt>"[%-0]"</tt> , or
a character class expression, such as <tt>"[[:punct:]]"</tt></p>
</li>
</ul>
<p>It is unspecified whether an explicit period in a bracket expression matching list, such as <tt>"[.abc]"</tt> , can match a
leading period in a filename.</p>
</li>
<li>
<p>Specified patterns shall be matched against existing filenames and pathnames, as appropriate. Each component that contains a
pattern character shall require read permission in the directory containing that component. Any component, except the last, that
does not contain a pattern character shall require search permission. For example, given the pattern:</p>
<blockquote>
<pre>
<tt>/foo/bar/x*/bam
</tt>
</pre>
</blockquote>
<p>search permission is needed for directories <b>/</b> and <b>foo</b>, search and read permissions are needed for directory
<b>bar</b>, and search permission is needed for each <b>x*</b> directory. If the pattern matches any existing filenames or
pathnames, the pattern shall be replaced with those filenames and pathnames, sorted according to the collating sequence in effect
in the current locale. If the pattern contains an invalid bracket expression or does not match any existing filenames or pathnames,
the pattern string shall be left unchanged.</p>
</li>
</ol>
<h3><a name="tag_02_14"></a>Special Built-In Utilities</h3>
<p>The following &quot;<a href="../idx/sbi.html">special built-in</a>&quot; utilities shall be supported in the shell command language. The output of each command, if
any, shall be written to standard output, subject to the normal redirection and piping possible with all commands.</p>
<p>The term &quot;built-in&quot; implies that the shell can execute the utility directly and does not need to search for it. An
implementation may choose to make any utility a built-in; however, the special built-in utilities described here differ from
regular built-in utilities in two respects:</p>
<ol>
<li>
<p>A syntax error in a special built-in utility may cause a shell executing that utility to abort, while a syntax error in a
regular built-in utility shall not cause a shell executing that utility to abort. (See <a href="#tag_02_08_01">Consequences of
Shell Errors</a> for the consequences of errors on interactive and non-interactive shells.) If a special built-in utility
encountering a syntax error does not abort the shell, its exit value shall be non-zero.</p>
</li>
<li>
<p>Variable assignments specified with special built-in utilities remain in effect after the built-in completes; this shall not be
the case with a regular built-in or other utility.</p>
</li>
</ol>
<p>The special built-in utilities in this section need not be provided in a manner accessible via the <i>exec</i> family of
functions defined in the System Interfaces volume of IEEE&nbsp;Std&nbsp;1003.1-2001.</p>
<p>Some of the special built-ins are described as conforming to the Base Definitions volume of IEEE&nbsp;Std&nbsp;1003.1-2001, <a
href="../basedefs/xbd_chap12.html#tag_12_02">Section 12.2, Utility Syntax Guidelines</a>. For those that are not, the requirement
in <a href="xcu_chap01.html#tag_01_11"><i>Utility Description Defaults</i></a> that <tt>"--"</tt> be recognized as a first argument
to be discarded does not apply and a conforming application shall not use that argument.</p>
<hr size="2" noshade>
<center><font size="2"><!--footer start-->
UNIX &reg; is a registered Trademark of The Open Group.<br>
POSIX &reg; is a registered Trademark of The IEEE.<br>
[ <a href="../mindex.html">Main Index</a> | <a href="../basedefs/contents.html">XBD</a> | <a href=
"../utilities/contents.html">XCU</a> | <a href="../functions/contents.html">XSH</a> | <a href="../xrat/contents.html">XRAT</a>
]</font></center>
<!--footer end-->
<hr size="2" noshade>
</body>
</html>