📄 chap2.html
字号:
<dt>${<i>parameter</i>##<i>word</i>}<dd><code><pre>x=/one/two/threeecho ${x##*/}<b>three</b></code></pre></dl><p>The double-quoting of patterns is different depending on where thedouble-quotes are placed:<dl compact><dt>${x#*}<dd>The asterisk is a pattern character.<dt>${x#"*"}<dd>The literal asterisk is quoted and not special.</dl><h4><a name = "tag_001_006_003"> </a>Command Substitution</h4><xref type="3" name="cmdsub"></xref>Command substitutionallows the output of a command to be substitutedin place of the command name itself.Command substitution occurs when the command is enclosed as follows:<pre><code>$(<i>command</i>)</code></pre>or (backquoted version):<pre><code>`<i>command</i>`</code></pre><p>The shell will expand the command substitutionby executing<i>command</i>in a subshell environment (see<xref href=shexenv><a href="#tag_001_012">Shell Execution Environment</a></xref>)and replacing the command substitution(the text of<i>command</i>plus the enclosing$()or backquotes)with the standard output of the command,removing sequences of one or morenewline charactersat the end of the substitution.Embeddednewline charactersbefore the end of the output will not be removed;however, they may be treated as field delimiters and eliminatedduring field splitting,depending on the value ofand quoting that is in effect.<p>Within the backquoted style of command substitution,backslash shall retain its literal meaning, except when followed by:<pre><code>$ ` \</code></pre><p>(dollar-sign, backquote, backslash).The search for the matching backquoteis satisfied by the first backquotefound without a preceding backslash;during this search,if a non-escaped backquote is encounteredwithin 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- or double-quoted string that begins, but does not end, within the`...` sequence produces undefined results.<p>With the $(<i>command</i>)form, all characters following the open parenthesis to the matchingclosing parenthesis constitute the<i>command</i>.Any valid shell script can be used for<i>command</i>,except:<ul><p><li>a script consisting solely of redirections produces unspecified results<p><li>see the restriction on single subshells described below.<p></ul><p>The results of command substitution will not befield splitting and pathname expansionprocessed for furthertilde expansion, parameter expansion, command substitutionor arithmetic expansion.If a command substitution occurs inside double-quotes, itwill not be performed on the results of the substitution.<p>Command substitution can be nested.To specify nesting within the backquoted version,the application must precede the inner backquotes withbackslashes;for example:<pre><code>\`<i>command</i>\`</code></pre><p>The$()form of command substitutionsolves a problem of inconsistent behaviour when using backquotes.For example:<p><table bordercolor=#000000 border=1 align=center><tr valign=top><th align=center><b>Command</b><th align=center><b>Output</b><tr valign=top><td align=left>echo '\$x'<td align=left>\$x<tr valign=top><td align=left>echo `echo '\$x'`<td align=left>$x<tr valign=top><td align=left>echo $(echo '\$x')<td align=left>\$x</table><p>Additionally, the backquoted syntax has historical restrictionson the contents of the embedded command.While the new$()form can process any kind of valid embedded script,the backquoted form cannot handle some valid scripts thatinclude backquotes.For example, these otherwise valid embedded scriptsdo not work in the left column, but do work on the right:<pre><code>echo ` echo $(cat <<\eof cat <<\eofa here-doc with ` a here-doc with )eof eof` )echo ` echo $(echo abc # a comment with ` echo abc # a comment with )` )echo ` echo $(echo '`' echo ')'` )</code></pre><p>Because of these inconsistent behaviours,the backquoted variety of command substitutionis not recommended for new applications that nest command substitutionsor attempt to embed complex scripts.<p>If the command substitution consists of a single subshell, such as:<pre><code>$( (<i>command</i>) )</code></pre>a portable application must separate the$(and"("into two tokens (that is, separate them with white space).This is required toavoid any ambiguities with arithmetic expansion.<h4><a name = "tag_001_006_004"> </a>Arithmetic Expansion</h4><xref type="3" name="aritexp"></xref>Arithmetic expansion provides a mechanism for evaluatingan arithmetic expression and substituting its value.The format for arithmetic expansion is as follows:<pre><code>$((<i>expression</i>))</code></pre><p>The expression is treated as if it were in double-quotes,except that a double-quote inside the expression is nottreated specially.The shell will expand all tokens in the expressionfor parameter expansion, command substitutionand quote removal.<p>Next, the shell will treat this as an arithmetic expressionand substitute the value of the expression.The arithmetic expression will be processedaccording to the rules of the ISO C standard,with the following exceptions:<ul><p><li>Only integer arithmetic is required.<p><li>Thesizeof()operator and the prefix and postfix++and--operators are not required.<p><li>Selection, iteration and jump statements are not supported.<p></ul><p>As an extension, the shell may recognise arithmetic expressionsbeyond those listed.If the expression is invalid, theexpansion will fail and the shellwill write a message to standard error indicating the failure.<p>A simple example using arithmetic expansion:<pre><code># repeat a command 100 timesx=100while [ $x -gt 0 ]do <i>command</i> x=$(($x-1))done</code></pre><h4><a name = "tag_001_006_005"> </a>Field Splitting</h4><xref type="3" name="fldspl"></xref>After parameter expansion,command substitution,and arithmetic expansion the shell will scan the results of expansions and substitutionsthat did not occur in double-quotes forfield splitting and multiple fields can result.<p>The shell will treat each character of the<i>IFS</i>as a delimiter and use the delimiters to split the results ofparameter expansion and command substitution intofields.<ol><p><li>If the value of<i>IFS</i>is a space, tab and newline character, or if it is unset,any sequence of space, tab or newlinecharacters at the beginning or end of the inputwill be ignored and any sequenceof those characters within the input will delimit a field.For example, the input:<pre><code><newline><space><tab>foo<tab><tab>bar<space></code></pre>yields two fields,<b>foo</b>and<b>bar</b>.<p><li>If the value of<i>IFS</i>is null, no field splitting will be performed.<p><li>Otherwise, the following rules will be applied in sequence.The term"<i>IFS</i>white space" is used to mean any sequence (zero or more instances) ofwhite-space characters that are in the<i>IFS</i>value (for example, if<i>IFS</i>contains space/comma/tab, any sequence of space and tabcharacters is considered<i>IFS</i>white space).<ol type = a><p><li><i>IFS</i>white space is ignored at the beginning and endof the input.<p><li>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,will delimit a field, as described previously.<p><li>Non-zero-length<i>IFS</i>white space will delimit a field.<p></ol><p></ol><p>The last rule can be summarised as a pseudo-ERE:<pre><code>(<i>s</i>*<i>ns</i>*|<i>s</i>+)</code></pre>where <i>s</i> is anwhite-space character and <i>n</i> is a character in thethat is not white space.Any string matching that ERE delimits a field, except that the <i>s</i>+form does not delimit fields at the beginning or the end of a line.For example, if<i>IFS</i>is space/comma/tab, the string:<pre><code><space><space>red<space><space>,<space>white<space>blue</code></pre>yields the three colours as the delimited fields.<h4><a name = "tag_001_006_006"> </a>Pathname Expansion</h4><xref type="3" name="pathexp"></xref>After field splitting, if<i>set</i><b>-f</b>is not in effect,each field in the resulting command line will beexpanded using the algorithm described in<xref href=patmat><a href="#tag_001_013">Pattern Matching Notation</a></xref>,qualified by the rules in<xref href=patfilx><a href="#tag_001_013_003">Patterns Used for Filename Expansion</a></xref>.<h4><a name = "tag_001_006_007"> </a>Quote Removal</h4><xref type="3" name="quoterm"></xref>The quote characters:<pre><code>\ ' "</code></pre><p>(backslash, single-quote, double-quote)that were present in the original wordwill be removed unless they have themselves been quoted.<hr size=2 noshade><h3><a name = "tag_001_007"> </a>Redirection</h3><xref type="2" name="redir"></xref>Redirection is used to open and close files for the currentshell execution environment (see<xref href=shexenv><a href="#tag_001_012">Shell Execution Environment</a></xref>)or for any command.<i>Redirection operators</i>can be used with numbers representingfile descriptors (see the definition in the ISO POSIX-1 standard)as described below.<p>The overall format used for redirection is:<code><p><b>[</b><i>n</i><b>]</b><i>redir-op</i> <i>word</i></code><p>The number<i>n</i>is an optional decimal number designatingthe file descriptor number; it must be delimitedfrom any preceding text and immediately precede theredirection operator<i>redir-op</i>.If<i>n</i>is quoted, the number will not be recognised as part of theredirection expression.For example:<pre><code>echo \2>a</code></pre>writes the character2into file <b>a</b>.If any part of<i>redir-op</i>is quoted, no redirection expression will be recognised.For example:<pre><code>echo 2\>a</code></pre>writes the characters2>ato standard output.The optional number, redirection operator and<i>word</i>will not appear in thearguments provided to the command to be executed (if any).<p>Open files are represented by decimal numbersstarting with zero.The largest possible value is implementation-dependent;however, all implementations support at least 0 to 9, inclusive,for use by the application.These numbers are called<i>file descriptors</i>.The values 0, 1 and 2have special meaning and conventional usesand 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 byone or more digits (withno interveningblank charactersallowed)to designate the file descriptor number.<p>If the redirection operator is<<or<<-,the word that follows the redirection operator will be subjected toquote removal;it is unspecified whether any of the other expansions occur.For the other redirection operators,the word that follows the redirection operator will be subjected totilde expansion, parameter expansion, command substitution,arithmetic expansion and quote removal.Pathname expansion will not be performed on the word by a non-interactive shell;an interactive shell may perform it, but will do so only when theexpansion would result in one word.<p>If more than one redirection operator is specifiedwith a command, the order of evaluation is from beginning to end.<p>A failure to open or create a file will cause the redirection to fail.<h4><a name = "tag_001_007_001"> </a>Redirecting Input</h4>Input redirection will cause the file whose name results from the expansion of<i>word</i>to be opened for readingon the designated file descriptor, or standard input ifthe file descriptor is not specified.<p>The general format for redirecting input is:<pre><code><b>[</b><i>n</i><b>]</b><<i>word</i></code></pre>where the optional<i>n</i>represents the file descriptor number.If the number is
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -