📄 chap2.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"><html><head><!-- Copyright 1997 The Open Group, All Rights Reserved --><title>Shell Command Language</title></head><body bgcolor=white><center><font size=2>The Single UNIX ® Specification, Version 2<br>Copyright © 1997 The Open Group</font></center><hr size=2 noshade><blockquote><center><h2><a name = "tag_001"> </a>Shell Command Language</h2></center><xref type="1" name="shell"></xref>This chapter contains the definition of theXSI Shell Command Language. See the <a href ="shellix.html">Shell Command Language Index</a> foran overview.<h3><a name = "tag_001_001"> </a>Shell Introduction</h3>The shell is a command language interpreter.This chapter describes the syntax of thatcommand language as it is used by the<i><a href="sh.html">sh</a></i>utility and the<i><a href="../xsh/system.html">system()</a></i>and<i><a href="../xsh/popen.html">popen()</a></i>functions in the <b>XSH</b> specification.<p>The shell operates according to the following general overviewof operations.The specific details are included in the cited sectionsof this chapter.<ol><p><li>The shell reads its input from a file (see<i><a href="sh.html">sh</a></i>),from the<b>-c</b>optionor from the<i><a href="../xsh/system.html">system()</a></i>and<i><a href="../xsh/popen.html">popen()</a></i>functions in the <b>XSH</b> specification.If the first lineof a file of shell commands starts with the characters#!,the resultsare unspecified.<p>The construct#!is reserved for implementations wishingto provide that extension.A portable application cannot use#!as the first line of a shell script;it might not be interpreted as a comment.<p><li>The shell breaks the input into tokens:words and operators.(See<xref href=token><a href="#tag_001_003">Token Recognition</a></xref>.)<p><li>The shell parses the input into simple commands(see<xref href=simpcmd><a href="#tag_001_009_001">Simple Commands</a></xref>)and compound commands(see<xref href=compcmd><a href="#tag_001_009_004">Compound Commands</a></xref>).<p><li>The shell performs various expansions(separately) on different parts of each command, resulting in alist of pathnames and fields to be treated as a commandand arguments (see<xref href=wordexp><a href="#tag_001_006">Word Expansions</a></xref>).<p><li>The shell performs redirection (see<xref href=redir><a href="#tag_001_007">Redirection</a></xref>)and removes redirection operators and their operands from theparameter list.<p><li>The shell executes a function (see<xref href=funccmd><a href="#tag_001_009_005">Function Definition Command</a></xref>),built-in (see<xref href=sbi><a href="#tag_001_014">Special Built-in Utilities</a></xref>),executable file or script, giving thenames of the arguments as positionalparameters numbered 1 to<i>n</i>,and thename of the command (or in the case ofa function within a script, the nameof the script) as the positionalparameter numbered 0 (see<xref href=cmdsea><a href="#tag_001_009_001_001">Command Search and Execution</a></xref>).<p><li>The shell optionally waits for the command tocomplete and collects the exit status (see<xref href=existat><a href="#tag_001_008_002">Exit Status for Commands</a></xref>).<p></ol><hr size=2 noshade><h3><a name = "tag_001_002"> </a>Quoting</h3><xref type="2" name="quoting"></xref>Quoting is used to remove the special meaning of certaincharacters or words to the shell.Quoting can be used to preservethe literal meaning ofthe special characters in the next paragraph; prevent reservedwords from being recognised as such; and prevent parameter expansionand command substitution within here-document processing (see<xref href=heredoc><a href="#tag_001_007_004">Here-document</a></xref>).<p>The following characters must be quoted if they are torepresent themselves:<pre><code>| & ; < > ( ) $ ` \ " ' <space> <tab> <newline></code></pre>and the following may need to be quoted under certain circumstances.That is, these characters may be special depending onconditions described elsewhere in this specification:<pre><code>* ? [ # ~ = %</code></pre><p>The various quoting mechanisms are the escape character,single-quotes and double-quotes.The here-document represents another form of quoting; see<xref href=heredoc><a href="#tag_001_007_004">Here-document</a></xref>.<h4><a name = "tag_001_002_001"> </a>Escape Character (Backslash)</h4><xref type="3" name="esc"></xref>A backslash that is not quotedpreserves the literal value of the following character,with the exception of anewline character.If anewline characterfollows thebackslash,the shell will interpret this as line continuation.The backslash andnewline characterswill be removed before splitting the input into tokens.Since the escapednewline characteris removed entirely from the inputand is not replaced by any white space,it cannot serve as a token separator.<h4><a name = "tag_001_002_002"> </a>Single-quotes</h4>Enclosing characters in single-quotes(' ')preserves the literal value of each character within the single-quotes.A single-quote cannot occur withinsingle-quotes.<p>A backslash cannot be used to escape a single-quotein a single-quoted string.An embeddedquote can be created by writing, for example:'a'\"b',which yieldsa'b.(See<xref href=fldspl><a href="#tag_001_006_005">Field Splitting</a></xref>for a better understanding of how portions of words are eithersplit into fields or remain concatenated.)A single token can be made upof concatenated partial strings containingall three kinds of quoting or escaping,thus permitting any combination of characters.<h4><a name = "tag_001_002_003"> </a>Double-quotes</h4><xref type="3" name="dquote"></xref>Enclosing characters in double-quotes(<b>" "</b>)preserves the literal value of all characters within the double-quotes,with the exception of the charactersdollar-sign, backquote and backslash, as follows:<dl compact><dt>$<dd>The dollar-sign retains its special meaning introducingparameter expansion (see<xref href=parmexp><a href="#tag_001_006_002">Parameter Expansion</a></xref>),a form of command substitution (see<xref href=cmdsub><a href="#tag_001_006_003">Command Substitution</a></xref>),and arithmetic expansion (see<xref href=aritexp><a href="#tag_001_006_004">Arithmetic Expansion</a></xref>).The input characters within the quoted string that are also enclosedbetween "$(" and the matching "("will not be affected by thedouble-quotes, but ratherdefine that command whose output replaces the$(...)when the word is expanded.The tokenising rules in<xref href=token><a href="#tag_001_003">Token Recognition</a></xref>are applied recursively to find the matching ")".Within the string of characters from an enclosed${to the matching"}",an even number ofunescaped double-quotes or single-quotes, if any, must occur.A preceding backslash character must be used to escape a literal"{"or"}".The rule in<xref href=parmexp><a href="#tag_001_006_002">Parameter Expansion</a></xref>will be used to determine the matching"}".<dt>`<dd>The backquote retains its special meaning introducingthe other form of command substitution (see<xref href=cmdsub><a href="#tag_001_006_003">Command Substitution</a></xref>).The portion of the quoted string from the initial backquoteand the characters up to the next backquotethat is not preceded by a backslash,having escape characters removed,defines that command whose output replaces `...`when the word is expanded.Either of the following cases produces undefined results:<ul><li>a single- or double-quoted string that begins, but does not end, withinthe `...` sequence<li>a `...` sequence that begins, but does not end, within thesame double-quoted string.</ul><dt>\<dd>The backslash retains its special meaning as an escape character (see<xref href=esc><a href="#tag_001_002_001">Escape Character (Backslash)</a></xref>)only when followed by one of the characters:<pre><code>$ ` " \ <newline></code></pre></dl><p>A double-quote must be preceded by a backslashto be included within double-quotes.The parameter @ has special meaning inside double-quotesand is described in<xref href=specpar><a href="#tag_001_005_002">Special Parameters</a></xref>.<p>In double-quoting, if a backslash isimmediately followed by a character that would be interpreted as having aspecial meaning, the backslash is deleted andthe 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 characterimmediately following it is also left unmodified.Thus, for example:<pre><code>"\$" -> $"\a" -> \a</code></pre>The requirement that double-quotes be matched inside${...}within double-quotes and the rule for finding the matching"}"in<xref href=parmexp><a href="#tag_001_006_002">Parameter Expansion</a></xref>eliminate severalsubtle inconsistencies in expansion for historical shells in rare cases;for example:<pre><code>"${foo-bar"}</code></pre>yields<b>bar</b>when<b>foo</b>is not defined, and is an invalid substitution when<b>foo</b>is defined, in many historical shells.The differences in processing the${...}form have led to inconsistencies betweenhistorical systems.A consequence of this rule is that single-quotescannot be used to quote the"}"within${...};for example:<pre><code>unset barfoo="${bar-'}'}"</code></pre>is invalid because the${...}substitution contains an unpaired unescaped single-quote.The backslash can be used to escape the"}"in this example to achieve the desired result:<pre><code>unset barfoo="${bar-\}}"</code></pre><p>Some systems have allowed the end of the word to terminatethe backquoted command substitution, such as in:<pre><code>"`echo hello"</code></pre><p>This usage is undefined; the matchingbackquote is required by this specification.The other undefined usage can be illustrated by the example:<pre><code>sh -c '` echo "foo`'</code></pre>The description of the recursive actions involving command substitutioncan be illustrated with an example.Upon recognising the introduction of command substitution, theshell must parse input (in a new context), gathering thesource for the command substitution until an unbalanced ")"or ` is located.For example, in the following:<pre><code>echo "$(date; echo " one" )"</code></pre>the double-quote following the<i><a href="echo.html">echo</a></i>does not terminate the first double-quote;it is part of the command substitution script.Similarly, in:<pre><code>echo "$(echo *)"</code></pre>the asterisk is not quoted since it is inside commandsubstitution; however:<pre><code>echo "$(echo "*")"</code></pre>is quoted (and represents the asterisk character itself).<hr size=2 noshade><h3><a name = "tag_001_003"> </a>Token Recognition</h3><xref type="2" name="token"></xref>The shell reads its input in terms of lines from a file, from aterminal in the case of an interactive shellor from a string in the case of<i><a href="sh.html">sh</a></i><b>-c</b>or<i><a href="../xsh/system.html">system()</a></i>.The input lines can be of unlimited length.These lines are parsed using two major modes:ordinary token recognitionand processing of here-documents.<p>When an<b>io_here</b>token has been recognised by the grammar(see<xref href=shgram><a href="#tag_001_010">Shell Grammar</a></xref>),one or more of thesubsequent lines immediately following the next<b>NEWLINE</b>tokenform the body of one or more here-documentsand are parsed according to the rules of<xref href=heredoc><a href="#tag_001_007_004">Here-document</a></xref>.<p>When it is not processing an<b>io_here</b>,the shell will break its input into tokens by applying the first applicablerule below to the next character in its input.The token will be from thecurrent position in the input until a token is delimited according toone of the rules below; the characters forming the token are exactly thosein the input, including any quoting characters.If it is indicated that a token is delimited, andno characters have been included in a token, processing will continueuntil an actual token is delimited.<ol><p><li>If the end of input is recognised, the current token will be delimited.If there is no current token, the end-of-input indicator will be returnedas the token.<p><li>If the previous character was used as part of an operator andthe current character is not quoted and can be usedwith the current characters to form anoperator, it will be used as part of that (operator) token.<p>Note that certain combinations of characters are invalidin portable scripts, as shown in the grammar, and that somesystems have assigned these combinations (such as|&)as valid control operators.Portable scripts cannot rely on receiving errors
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -