📄 xcu_chap02.html
字号:
<div class="box"><em>The following sections are informative.</em></div><h5><a name="tag_02_06_02_01"></a>Examples</h5><dl compact><dt>${<i>parameter</i>:-<i>word</i>}</dt><dd><br>In this example, <a href="../utilities/ls.html"><i>ls</i></a> is executed only if <i>x</i> is null or unset. (The $( <a href="../utilities/ls.html"><i>ls</i></a>) command substitution notation is explained in <a href="#tag_02_06_03">CommandSubstitution</a> .) <blockquote><pre><tt>${x:-$(ls)}</tt></pre></blockquote></dd><dt>${<i>parameter</i>:=<i>word</i>}</dt><dd><pre><tt>unset Xecho ${X:=abc}</tt><b>abc</b></pre></dd><dt>${<i>parameter</i>:?<i>word</i>}</dt><dd><pre><tt>unset posixecho ${posix:?}</tt><b>sh: posix: parameter null or not set</b></pre></dd><dt>${<i>parameter</i>:+<i>word</i>}</dt><dd><pre><tt>set a b cecho ${3:+posix}</tt><b>posix</b></pre></dd><dt>${#<i>parameter</i>}</dt><dd><pre><tt>HOME=/usr/posixecho ${#HOME}</tt><b>10</b></pre></dd><dt>${<i>parameter</i>%<i>word</i>}</dt><dd><pre><tt>x=file.cecho ${x%.c}.o</tt><b>file.o</b></pre></dd><dt>${<i>parameter</i>%%<i>word</i>}</dt><dd><pre><tt>x=posix/src/stdecho ${x%%/*}</tt><b>posix</b></pre></dd><dt>${<i>parameter</i>#<i>word</i>}</dt><dd><pre><tt>x=$HOME/src/cmdecho ${x#$HOME}</tt><b>/src/cmd</b></pre></dd><dt>${<i>parameter</i>##<i>word</i>}</dt><dd><pre><tt>x=/one/two/threeecho ${x##*/}</tt><b>three</b></pre></dd></dl><p>The double-quoting of patterns is different depending on where the double-quotes are placed:</p><dl compact><dt><tt>"${x#*}"</tt></dt><dd>The asterisk is a pattern character.</dd><dt><tt>${x#"*"}</tt></dt><dd>The literal asterisk is quoted and not special.</dd></dl><div class="box"><em>End of informative text.</em></div><hr><h4><a name="tag_02_06_03"></a>Command Substitution</h4><p>Command substitution allows the output of a command to be substituted in place of the command name itself. Command substitutionshall occur when the command is enclosed as follows:</p><blockquote><pre><tt>$(</tt><i>command</i><tt>)</tt></pre></blockquote><p>or (backquoted version):</p><blockquote><pre><tt>`</tt><i>command</i><tt>`</tt></pre></blockquote><p>The shell shall expand the command substitution by executing <i>command</i> in a subshell environment (see <a href="#tag_02_12">Shell Execution Environment</a> ) and replacing the command substitution (the text of <i>command</i> plus theenclosing <tt>"$()"</tt> or backquotes) with the standard output of the command, removing sequences of one or more <newline>sat the end of the substitution. Embedded <newline>s before the end of the output shall not be removed; however, they may betreated as field delimiters and eliminated during field splitting, depending on the value of <i>IFS</i> and quoting that is ineffect.</p><p>Within the backquoted style of command substitution, backslash shall retain its literal meaning, except when followed by:<tt>'$'</tt> , <tt>'`'</tt> , or <tt>'\'</tt> (dollar sign, backquote, backslash). The search for the matching backquote shall besatisfied by the first backquote found 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-quoted or double-quoted string that begins, but does not end, within the <tt>"`...`"</tt>sequence produces undefined results.</p><p>With the $( <i>command</i>) form, all characters following the open parenthesis to the matching closing parenthesis constitutethe <i>command</i>. Any valid shell script can be used for <i>command</i>, except a script consisting solely of redirections whichproduces unspecified results.</p><p>The results of command substitution shall not be processed for further tilde expansion, parameter expansion, commandsubstitution, or arithmetic expansion. If a command substitution occurs inside double-quotes, field splitting and pathnameexpansion shall not be performed on the results of the substitution.</p><p>Command substitution can be nested. To specify nesting within the backquoted version, the application shall precede the innerbackquotes with backslashes, for example:</p><blockquote><pre><tt>\`</tt><i>command</i><tt>\`</tt></pre></blockquote><p>If the command substitution consists of a single subshell, such as:</p><blockquote><pre><tt>$( (</tt><i>command</i><tt>) )</tt></pre></blockquote><p>a conforming application shall separate the <tt>"$("</tt> and <tt>'('</tt> into two tokens (that is, separate them with whitespace). This is required to avoid any ambiguities with arithmetic expansion.</p><h4><a name="tag_02_06_04"></a>Arithmetic Expansion</h4><p>Arithmetic expansion provides a mechanism for evaluating an arithmetic expression and substituting its value. The format forarithmetic expansion shall be as follows:</p><blockquote><pre><tt>$((</tt><i>expression</i><tt>))</tt></pre></blockquote><p>The expression shall be treated as if it were in double-quotes, except that a double-quote inside the expression is not treatedspecially. The shell shall expand all tokens in the expression for parameter expansion, command substitution, and quoteremoval.</p><p>Next, the shell shall treat this as an arithmetic expression and substitute the value of the expression. The arithmeticexpression shall be processed according to the rules given in <a href="xcu_chap01.html#tag_01_07_02_01"><i>Arithmetic Precision andOperations</i></a> , with the following exceptions:</p><ul><li><p>Only signed long integer arithmetic is required.</p></li><li><p>Only the decimal-constant, octal-constant, and hexadecimal-constant constants specified in the ISO C standard, Section6.4.4.1 are required to be recognized as constants.</p></li><li><p>The <i>sizeof</i>() operator and the prefix and postfix <tt>"++"</tt> and <tt>"--"</tt> operators are not required.</p></li><li><p>Selection, iteration, and jump statements are not supported.</p></li></ul><p>As an extension, the shell may recognize arithmetic expressions beyond those listed. The shell may use a signed integer typewith a rank larger than the rank of <b>signed long</b>. The shell may use a real-floating type instead of <b>signed long</b> aslong as it does not affect the results in cases where there is no overflow. If the expression is invalid, the expansion fails andthe shell shall write a message to standard error indicating the failure.</p><hr><div class="box"><em>The following sections are informative.</em></div><h5><a name="tag_02_06_04_01"></a>Examples</h5><p>A simple example using arithmetic expansion:</p><blockquote><pre><tt># repeat a command 100 timesx=100while [ $x -gt 0 ]do </tt> <i>command</i> <tt> x=$(($x-1))done</tt></pre></blockquote><div class="box"><em>End of informative text.</em></div><hr><h4><a name="tag_02_06_05"></a>Field Splitting</h4><p>After parameter expansion ( <a href="#tag_02_06_02">Parameter Expansion</a> ), command substitution ( <a href="#tag_02_06_03">Command Substitution</a> ), and arithmetic expansion ( <a href="#tag_02_06_04">Arithmetic Expansion</a> ), theshell shall scan the results of expansions and substitutions that did not occur in double-quotes for field splitting and multiplefields can result.</p><p>The shell shall treat each character of the <i>IFS</i> as a delimiter and use the delimiters to split the results of parameterexpansion and command substitution into fields.</p><ol><li><p>If the value of <i>IFS</i> is a <space>, <tab>, and <newline>, or if it is unset, any sequence of<space>s, <tab>s, or <newline>s at the beginning or end of the input shall be ignored and any sequence of thosecharacters within the input shall delimit a field. For example, the input:</p><blockquote><pre><tt><newline><space><tab>foo<tab><tab>bar<space></tt></pre></blockquote><p>yields two fields, <b>foo</b> and <b>bar</b>.</p></li><li><p>If the value of <i>IFS</i> is null, no field splitting shall be performed.</p></li><li><p>Otherwise, the following rules shall be applied in sequence. The term " <i>IFS</i> white space" is used to mean any sequence(zero or more instances) of white space characters that are in the <i>IFS</i> value (for example, if <i>IFS</i> contains<space>/ <comma>/ <tab>, any sequence of <space>s and <tab>s is considered <i>IFS</i> whitespace).</p><ol type="a"><li><p><i>IFS</i> white space shall be ignored at the beginning and end of the input.</p></li><li><p>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, shall delimit a field, as described previously.</p></li><li><p>Non-zero-length <i>IFS</i> white space shall delimit a field.</p></li></ol></li></ol><h4><a name="tag_02_06_06"></a>Pathname Expansion</h4><p>After field splitting, if <a href="set.html"><i>set</i></a> <b>-f</b> is not in effect, each field in the resulting command lineshall be expanded using the algorithm described in <a href="#tag_02_13">Pattern Matching Notation</a> , qualified by the rules in<a href="#tag_02_13_03">Patterns Used for Filename Expansion</a> .</p>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -