⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 xcu_chap02.html

📁 IEEE 1003.1-2003, Single Unix Specification v3
💻 HTML
📖 第 1 页 / 共 5 页
字号:
<!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>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, 2003 Edition<br>Copyright &copy; 2001-2003 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 <ahref="../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 ofIEEE&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 citedsections 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 definedin the System Interfaces volume of IEEE&nbsp;Std&nbsp;1003.1-2001. If the first line of a file of shell commands starts with thecharacters <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 <ahref="#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 andfields 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 operandsfrom 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 positionalparameters 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 thescript) 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 forCommands</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 theliteral meaning of the special characters in the next paragraph, prevent reserved words from being recognized as such, and preventparameter 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 onconditions 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 anotherform 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 entirelyfrom 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 thesingle-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 thedouble-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">ParameterExpansion</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> shallnot be affected by the double-quotes, but rather shall define that command whose output replaces the <tt>"$(...)"</tt> when theword is expanded. The tokenizing rules in <a href="#tag_02_03">Token Recognition</a> , not including the alias substitutions in <ahref="#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 unescapeddouble-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 tothe next backquote that is not preceded by a backslash, having escape characters removed, defines that command whose outputreplaces <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 astring 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 twomajor 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 ofthe subsequent lines immediately following the next <b>NEWLINE</b> token form the body of one or more here-documents and shall beparsed 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 rulebelow to the next character in its input. The token shall be from the current position in the input until a token is delimitedaccording to one of the rules below; the characters forming the token are exactly those in the input, including any quotingcharacters. If it is indicated that a token is delimited, and no characters have been included in a token, processing shallcontinue 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-inputindicator 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 thecurrent 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 charactersto 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 isnot quoted, it shall affect quoting for subsequent characters up to the end of the quoted text. The rules for quoting are asdescribed in <a href="#tag_02_02">Quoting</a> . During token recognition no substitutions shall be actually performed, and theresult 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. Thetoken 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 forparameter expansion ( <a href="#tag_02_06_02">Parameter Expansion</a> ), command substitution ( <a href="#tag_02_06_03">CommandSubstitution</a> ), or arithmetic expansion ( <a href="#tag_02_06_04">Arithmetic Expansion</a> ) from their introductory unquotedcharacter sequences: <tt>'$'</tt> or <tt>"${"</tt> , <tt>"$("</tt> or <tt>'`'</tt> , and <tt>"$(("</tt> , respectively. The shellshall read sufficient input to determine the end of the unit to be expanded (as explained in the cited sections). While processingthe characters, if instances of expansions or quoting are found nested within the substitution, the shell shall recursively processthem in the manner specified for the construct that is found. The characters found from the beginning of the substitution to itsend, 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 thesubstitution.</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 currentcharacter 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 PortabilityUtilities 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> , aresulting word that is identified to be the command name word of a simple command shall be examined to determine whether it is anunquoted, 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 whenthe shell is invoked. To prevent infinite loops in recursive aliasing, if the shell is not currently processing an alias of thesame 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 aliassubstitution; 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 separateinvocations of the shell or by the utility execution environments invoked by the shell; see <a href="#tag_02_12">Shell ExecutionEnvironment</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 followingwords 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>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -