📄 xcu_chap02.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-2003 The Open Group, All Rights Reserved --><title>Rationale</title></head><body><basefont size="3"> <center><font size="2">The Open Group Base Specifications Issue 6<br>IEEE Std 1003.1, 2003 Edition<br>Copyright © 2001-2003 The IEEE and The Open Group</font></center><hr size="2" noshade><h3><a name="tag_02_02"></a>Shell Command Language</h3><h4><a name="tag_02_02_01"></a>Shell Introduction</h4><p>The System V shell was selected as the starting point for the Shell and Utilities volume of IEEE Std 1003.1-2001.The BSD C shell was excluded from consideration for the following reasons:</p><ul><li><p>Most historically portable shell scripts assume the Version 7 Bourne shell, from which the System V shell is derived.</p></li><li><p>The majority of tutorial materials on shell programming assume the System V shell.</p></li></ul><p>The construct <tt>"#!"</tt> is reserved for implementations wishing to provide that extension. If it were not reserved, theShell and Utilities volume of IEEE Std 1003.1-2001 would disallow it by forcing it to be a comment. As it stands, astrictly conforming application must not use <tt>"#!"</tt> as the first two characters of the file.</p><h4><a name="tag_02_02_02"></a>Quoting</h4><p>There is no additional rationale provided for this section.</p><h5><a name="tag_02_02_02_01"></a>Escape Character (Backslash)</h5><p>There is no additional rationale provided for this section.</p><h5><a name="tag_02_02_02_02"></a>Single-Quotes</h5><p>A backslash cannot be used to escape a single-quote in a single-quoted string. An embedded quote can be created by writing, forexample: <tt>"'a'\''b'"</tt> , which yields <tt>"a'b"</tt> . (See the Shell and Utilities volume of IEEE Std 1003.1-2001,<a href="../utilities/xcu_chap02.html#tag_02_06_05">Section 2.6.5, Field Splitting</a> for a better understanding of how portionsof words are either split into fields or remain concatenated.) A single token can be made up of concatenated partial stringscontaining all three kinds of quoting or escaping, thus permitting any combination of characters.</p><h5><a name="tag_02_02_02_03"></a>Double-Quotes</h5><p>The escaped <newline> used for line continuation is removed entirely from the input and is not replaced by any whitespace. Therefore, it cannot serve as a token separator.</p><p>In double-quoting, if a backslash is immediately followed by a character that would be interpreted as having a special meaning,the backslash is deleted and the subsequent character is taken literally. If a backslash does not precede a character that wouldhave a special meaning, it is left in place unmodified and the character immediately following it is also left unmodified. Thus,for example:</p><blockquote><pre><tt>"\$" -> $<br>"\a" -> \a</tt></pre></blockquote><p>It would be desirable to include the statement "The characters from an enclosed <tt>"${"</tt> to the matching <tt>'}'</tt>shall not be affected by the double quotes", similar to the one for <tt>"$()"</tt> . However, historical practice in theSystem V shell prevents this.</p><p>The requirement that double-quotes be matched inside <tt>"${...}"</tt> within double-quotes and the rule for finding thematching <tt>'}'</tt> in the Shell and Utilities volume of IEEE Std 1003.1-2001, <a href="../utilities/xcu_chap02.html#tag_02_06_02">Section 2.6.2, Parameter Expansion</a> eliminate several subtle inconsistencies inexpansion for historical shells in rare cases; for example:</p><blockquote><pre><tt>"${foo-bar"}</tt></pre></blockquote><p>yields <b>bar</b> when <b>foo</b> is not defined, and is an invalid substitution when <b>foo</b> is defined, in many historicalshells. The differences in processing the <tt>"${...}"</tt> form have led to inconsistencies between historical systems. Aconsequence of this rule is that single-quotes cannot be used to quote the <tt>'}'</tt> within <tt>"${...}"</tt> ; for example:</p><blockquote><pre><tt>unset barfoo="${bar-'}'}"</tt></pre></blockquote><p>is invalid because the <tt>"${...}"</tt> substitution contains an unpaired unescaped single-quote. The backslash can be used toescape the <tt>'}'</tt> in this example to achieve the desired result:</p><blockquote><pre><tt>unset barfoo="${bar-\}}"</tt></pre></blockquote><p>The differences in processing the <tt>"${...}"</tt> form have led to inconsistencies between the historical System V shell,BSD, and KornShells, and the text in the Shell and Utilities volume of IEEE Std 1003.1-2001 is an attempt to convergethem without breaking too many applications. The only alternative to this compromise between shells would be to make the behaviorunspecified whenever the literal characters <tt>'"</tt> , <tt>'{'</tt> , <tt>'}'</tt> , and <tt>''</tt> appear within<tt>"${...}"</tt> . To write a portable script that uses these values, a user would have to assign variables; for example:</p><blockquote><pre><tt>squote=\' dquote=\" lbrace='{' rbrace='}'${foo-$squote$rbrace$squote}</tt></pre></blockquote><p>rather than:</p><blockquote><pre><tt>${foo-"'}'"}</tt></pre></blockquote><p>Some implementations have allowed the end of the word to terminate the backquoted command substitution, such as in:</p><blockquote><pre><tt>"`echo hello"</tt></pre></blockquote><p>This usage is undefined; the matching backquote is required by the Shell and Utilities volume of IEEE Std 1003.1-2001.The other undefined usage can be illustrated by the example:</p><blockquote><pre><tt>sh -c '` echo "foo`'</tt></pre></blockquote><p>The description of the recursive actions involving command substitution can be illustrated with an example. Upon recognizing theintroduction of command substitution, the shell parses input (in a new context), gathering the source for the command substitutionuntil an unbalanced <tt>')'</tt> or <tt>'`'</tt> is located. For example, in the following:</p><blockquote><pre><tt>echo "$(date; echo " one" )"</tt></pre></blockquote><p>the double-quote following the <a href="../utilities/echo.html"><i>echo</i></a> does not terminate the first double-quote; it ispart of the command substitution script. Similarly, in:</p><blockquote><pre><tt>echo "$(echo *)"</tt></pre></blockquote><p>the asterisk is not quoted since it is inside command substitution; however:</p><blockquote><pre><tt>echo "$(echo "*")"</tt></pre></blockquote><p>is quoted (and represents the asterisk character itself).</p><h4><a name="tag_02_02_03"></a>Token Recognition</h4><p>The <tt>"(("</tt> and <tt>"))"</tt> symbols are control operators in the KornShell, used for an alternative syntax of anarithmetic expression command. A conforming application cannot use <tt>"(("</tt> as a single token (with the exception of the<tt>"$(("</tt> form for shell arithmetic).</p><p>On some implementations, the symbol <tt>"(("</tt> is a control operator; its use produces unspecified results. Applications thatwish to have nested subshells, such as:</p><blockquote><pre><tt>((echo Hello);(echo World))</tt></pre></blockquote><p>must separate the <tt>"(("</tt> characters into two tokens by including white space between them. Some systems may treat theseas invalid arithmetic expressions instead of subshells.</p><p>Certain combinations of characters are invalid in portable scripts, as shown in the grammar. Implementations may use thesecombinations (such as <tt>"|&"</tt> ) as valid control operators. Portable scripts cannot rely on receiving errors in all caseswhere this volume of IEEE Std 1003.1-2001 indicates that a syntax is invalid.</p><p>The (3) rule about combining characters to form operators is not meant to preclude systems from extending the shell languagewhen characters are combined in otherwise invalid ways. Conforming applications cannot use invalid combinations, and test suitesshould not penalize systems that take advantage of this fact. For example, the unquoted combination <tt>"|&"</tt> is not validin a POSIX script, but has a specific KornShell meaning.</p><p>The (10) rule about <tt>'#'</tt> as the current character is the first in the sequence in which a new token is being assembled.The <tt>'#'</tt> starts a comment only when it is at the beginning of a token. This rule is also written to indicate that thesearch for the end-of-comment does not consider escaped <newline> specially, so that a comment cannot be continued to thenext line.</p><h5><a name="tag_02_02_03_01"></a>Alias Substitution</h5><p>The alias capability was added in the User Portability Utilities option because it is widely used in historical implementationsby interactive users.</p><p>The definition of "alias name" precludes an alias name containing a slash character. Since the text applies to the commandwords of simple commands, reserved words (in their proper places) cannot be confused with aliases.</p><p>The placement of alias substitution in token recognition makes it clear that it precedes all of the word expansion steps.</p><p>An example concerning trailing <blank>s and reserved words follows. If the user types:</p><blockquote><pre><b>$</b> <tt>alias foo="/bin/ls "</tt><b>$</b> <tt>alias while="/"</tt></pre></blockquote><p>The effect of executing:</p><blockquote><pre><b>$</b> <tt>while true</tt><b>></b> <tt>do</tt><b>></b> <tt>echo "Hello, World"</tt><b>></b> <tt>done</tt></pre></blockquote><p>is a never-ending sequence of <tt>"Hello, World"</tt> strings to the screen. However, if the user types:</p><blockquote><pre><b>$</b> <tt>foo while</tt></pre></blockquote><p>the result is an <a href="../utilities/ls.html"><i>ls</i></a> listing of <b>/</b>. Since the alias substitution for <b>foo</b>ends in a <space>, the next word is checked for alias substitution. The next word, <b>while</b>, has also been aliased, so itis substituted as well. Since it is not in the proper position as a command word, it is not recognized as a reserved word.</p><p>If the user types:</p><blockquote><pre><b>$</b> <tt>foo; while</tt></pre></blockquote><p><b>while</b> retains its normal reserved-word properties.</p><h4><a name="tag_02_02_04"></a>Reserved Words</h4><p>All reserved words are recognized syntactically as such in the contexts described. However, note that <b>in</b> is the onlymeaningful reserved word after a <b>case</b> or <b>for</b>; similarly, <b>in</b> is not meaningful as the first word of a simplecommand.</p><p>Reserved words are recognized only when they are delimited (that is, meet the definition of the Base Definitions volume ofIEEE Std 1003.1-2001, <a href="../basedefs/xbd_chap03.html#tag_03_435">Section 3.435, Word</a>), whereas operators arethemselves delimiters. For instance, <tt>'('</tt> and <tt>')'</tt> are control operators, so that no <space> is needed in(<i>list</i>). However, <tt>'{'</tt> and <tt>'}'</tt> are reserved words in { <i>list</i>;}, so that in this case the leading<space> and semicolon are required.</p><p>The list of unspecified reserved words is from the KornShell, so conforming applications cannot use them in places a reservedword would be recognized. This list contained <b>time</b> in early proposals, but it was removed when the <a href="../utilities/time.html"><i>time</i></a> utility was selected for the Shell and Utilities volume ofIEEE Std 1003.1-2001.</p><p>There was a strong argument for promoting braces to operators (instead of reserved words), so they would be syntacticallyequivalent to subshell operators. Concerns about compatibility outweighed the advantages of this approach. Nevertheless, conformingapplications should consider quoting <tt>'{'</tt> and <tt>'}'</tt> when they represent themselves.</p><p>The restriction on ending a name with a colon is to allow future implementations that support named labels for flow control; seethe RATIONALE for the <a href="../utilities/break.html"><i>break</i></a> built-in utility.</p><p>It is possible that a future version of the Shell and Utilities volume of IEEE Std 1003.1-2001 may require that<tt>'{'</tt> and <tt>'}'</tt> be treated individually as control operators, although the token <tt>"{}"</tt> will probably be aspecial-case exemption from this because of the often-used <a href="../utilities/find.html"><i>find</i></a>{} construct.</p><h4><a name="tag_02_02_05"></a>Parameters and Variables</h4>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -