Skip to content
xcu_chap02.html 127 KiB
Newer Older
Seblu's avatar
Seblu committed
<!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="xcu_chap02_files/style.css"><!-- Generated by The Open Group's rhtm tool v1.2.1 --><!-- Copyright (c) 2001-2004 IEEE and The Open Group, All Rights Reserved --><title>Shell Command Language</title></head><body bgcolor="white">
<script type="text/javascript" language="JavaScript" src="xcu_chap02_files/codes.js">
</script>

<basefont size="3"> <!--header start-->
<center><font size="2">The Open Group Base Specifications Issue 6<br>
IEEE Std 1003.1, 2004 Edition<br>
Copyright © 2001-2004 The IEEE and The Open Group, All Rights reserved.</font></center>

<!--header end-->
<hr noshade="noshade" size="2">
<h2><a name="tag_02">  2. </a>Shell Command Language</h2>

<p>This chapter contains the definition of the Shell Command Language.</p>

<h3><a name="tag_02_01">   2.1 </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="http://www.opengroup.org/onlinepubs/009695399/utilities/sh.html"><i>sh</i></a> utility and the <a href="http://www.opengroup.org/onlinepubs/009695399/functions/system.html"><i>system</i>()</a> and <a href="http://www.opengroup.org/onlinepubs/009695399/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="http://www.opengroup.org/onlinepubs/009695399/utilities/sh.html"><i>sh</i></a>), from the <b>-c</b> option or from the
<a href="http://www.opengroup.org/onlinepubs/009695399/functions/system.html"><i>system</i>()</a> and <a href="http://www.opengroup.org/onlinepubs/009695399/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">   2.2 </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>*   ?   [   #   &#732;   =   %
</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">  2.2.1 </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">  2.2.2 </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">  2.2.3 </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="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">   2.3 </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="http://www.opengroup.org/onlinepubs/009695399/utilities/sh.html"><i>sh</i></a> <b>-c</b> or <a href="http://www.opengroup.org/onlinepubs/009695399/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>
Loading full blame...