📄 special-chars.html
字号:
></DD><DT><ANAME="BACKTICKSREF"></A><SPANCLASS="TOKEN">`</SPAN></DT><DD><DIVCLASS="FORMALPARA"><P><B><AHREF="commandsub.html#COMMANDSUBREF">command substitution</A>. </B>The <BCLASS="COMMAND">`command`</B> construct makes available the output of <BCLASS="COMMAND">command</B> for assignment to a variable. This is also known as <AHREF="commandsub.html#BACKQUOTESREF">backquotes</A> or backticks.</P></DIV></DD><DT><ANAME="COLON0REF"></A><SPANCLASS="TOKEN">:</SPAN></DT><DD><P><ANAME="NULLREF"></A></P><DIVCLASS="FORMALPARA"><P><B>null command [colon]. </B>This is the shell equivalent of a <SPANCLASS="QUOTE">"NOP"</SPAN> (<TTCLASS="REPLACEABLE"><I>no op</I></TT>, a do-nothing operation). It may be considered a synonym for the shell builtin <AHREF="internal.html#TRUEREF">true</A>. The <SPANCLASS="QUOTE">"<SPANCLASS="TOKEN">:</SPAN>"</SPAN> command is itself a <ICLASS="FIRSTTERM">Bash</I> <AHREF="internal.html#BUILTINREF">builtin</A>, and its <AHREF="exit-status.html#EXITSTATUSREF">exit status</A> is <SPANCLASS="QUOTE">"true"</SPAN> (<SPANCLASS="RETURNVALUE">0</SPAN>).</P></DIV><P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING"> 1 : 2 echo $? # 0</PRE></TD></TR></TABLE></P><P>Endless loop:</P><P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING"> 1 while : 2 do 3 operation-1 4 operation-2 5 ... 6 operation-n 7 done 8 9 # Same as: 10 # while true 11 # do 12 # ... 13 # done</PRE></TD></TR></TABLE> </P><P>Placeholder in if/then test:</P><P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING"> 1 if condition 2 then : # Do nothing and branch ahead 3 else 4 take-some-action 5 fi</PRE></TD></TR></TABLE> </P><P>Provide a placeholder where a binary operation is expected, see <AHREF="operations.html#ARITHOPS">Example 8-2</A> and <AHREF="parameter-substitution.html#DEFPARAM">default parameters</A>.</P><P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING"> 1 : ${username=`whoami`} 2 # ${username=`whoami`} Gives an error without the leading : 3 # unless "username" is a command or builtin...</PRE></TD></TR></TABLE></P><P>Provide a placeholder where a command is expected in a <AHREF="here-docs.html#HEREDOCREF">here document</A>. See <AHREF="here-docs.html#ANONHEREDOC">Example 18-10</A>.</P><P>Evaluate string of variables using <AHREF="parameter-substitution.html#PARAMSUBREF">parameter substitution</A> (as in <AHREF="parameter-substitution.html#EX6">Example 9-16</A>). <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING"> 1 : ${HOSTNAME?} ${USER?} ${MAIL?} 2 # Prints error message 3 #+ if one or more of essential environmental variables not set.</PRE></TD></TR></TABLE></P><P><BCLASS="COMMAND"><AHREF="parameter-substitution.html#EXPREPL1">Variable expansion / substring replacement</A></B>.</P><P>In combination with the <SPANCLASS="TOKEN">></SPAN> <AHREF="io-redirection.html#IOREDIRREF">redirection operator</A>, truncates a file to zero length, without changing its permissions. If the file did not previously exist, creates it. <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING"> 1 : > data.xxx # File "data.xxx" now empty. 2 3 # Same effect as cat /dev/null >data.xxx 4 # However, this does not fork a new process, since ":" is a builtin.</PRE></TD></TR></TABLE> See also <AHREF="textproc.html#EX12">Example 15-15</A>.</P><P>In combination with the <SPANCLASS="TOKEN">>></SPAN> redirection operator, has no effect on a pre-existing target file (<TTCLASS="USERINPUT"><B>: >> target_file</B></TT>). If the file did not previously exist, creates it.</P><DIVCLASS="NOTE"><TABLECLASS="NOTE"WIDTH="90%"BORDER="0"><TR><TDWIDTH="25"ALIGN="CENTER"VALIGN="TOP"><IMGSRC="common/note.png"HSPACE="5"ALT="Note"></TD><TDALIGN="LEFT"VALIGN="TOP"><P>This applies to regular files, not pipes, symlinks, and certain special files.</P></TD></TR></TABLE></DIV><P>May be used to begin a comment line, although this is not recommended. Using <SPANCLASS="TOKEN">#</SPAN> for a comment turns off error checking for the remainder of that line, so almost anything may appear in a comment. However, this is not the case with <SPANCLASS="TOKEN">:</SPAN>. <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING"> 1 : This is a comment that generates an error, ( if [ $x -eq 3] ).</PRE></TD></TR></TABLE> </P><P>The <SPANCLASS="QUOTE">"<SPANCLASS="TOKEN">:</SPAN>"</SPAN> also serves as a field separator, in <TTCLASS="FILENAME">/etc/passwd</TT>, and in the <AHREF="variables2.html#PATHREF">$PATH</A> variable. <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="SCREEN"> <TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>echo $PATH</B></TT> <TTCLASS="COMPUTEROUTPUT">/usr/local/bin:/bin:/usr/bin:/usr/X11R6/bin:/sbin:/usr/sbin:/usr/games</TT></PRE></TD></TR></TABLE> </P></DD><DT><ANAME="NOTREF"></A><SPANCLASS="TOKEN">!</SPAN></DT><DD><DIVCLASS="FORMALPARA"><P><B>reverse (or negate) the sense of a test or exit status [bang]. </B>The <SPANCLASS="TOKEN">!</SPAN> operator inverts the <AHREF="exit-status.html#EXITSTATUSREF">exit status</A> of the command to which it is applied (see <AHREF="exit-status.html#NEGCOND">Example 6-2</A>). It also inverts the meaning of a test operator. This can, for example, change the sense of <ICLASS="FIRSTTERM">equal</I> ( <AHREF="comparison-ops.html#EQUALSIGNREF">=</A> ) to <ICLASS="FIRSTTERM">not-equal</I> ( != ). The <SPANCLASS="TOKEN">!</SPAN> operator is a Bash <AHREF="internal.html#KEYWORDREF">keyword</A>.</P></DIV><P>In a different context, the <SPANCLASS="TOKEN">!</SPAN> also appears in <AHREF="ivr.html#IVRREF">indirect variable references</A>.</P><P>In yet another context, from the <ICLASS="FIRSTTERM">command line</I>, the <SPANCLASS="TOKEN">!</SPAN> invokes the Bash <ICLASS="FIRSTTERM">history mechanism</I> (see <AHREF="histcommands.html">Appendix J</A>). Note that within a script, the history mechanism is disabled.</P></DD><DT><ANAME="ASTERISKREF"></A><SPANCLASS="TOKEN">*</SPAN></DT><DD><DIVCLASS="FORMALPARA"><P><B>wild card [asterisk]. </B>The <SPANCLASS="TOKEN">*</SPAN> character serves as a <SPANCLASS="QUOTE">"wild card"</SPAN> for filename expansion in <AHREF="globbingref.html">globbing</A>. By itself, it matches every filename in a given directory.</P></DIV><P> <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="SCREEN"> <TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>echo *</B></TT> <TTCLASS="COMPUTEROUTPUT">abs-book.sgml add-drive.sh agram.sh alias.sh</TT> </PRE></TD></TR></TABLE> </P><P><ANAME="ASTERISKREF2"></A></P><P>The <SPANCLASS="TOKEN">*</SPAN> also represents any number (or zero) characters in a <AHREF="regexp.html#REGEXREF">regular expression</A>.</P></DD><DT><SPANCLASS="TOKEN">*</SPAN></DT><DD><DIVCLASS="FORMALPARA"><P><B><AHREF="operations.html#AROPS1">arithmetic operator</A>. </B>In the context of arithmetic operations, the <SPANCLASS="TOKEN">*</SPAN> denotes multiplication.</P></DIV><P>A double asterisk, <SPANCLASS="TOKEN">**</SPAN>, is the <AHREF="operations.html#EXPONENTIATIONREF">exponentiation operator</A>.</P></DD><DT><SPANCLASS="TOKEN">?</SPAN></DT><DD><DIVCLASS="FORMALPARA"><P><B>test operator. </B>Within certain expressions, the <SPANCLASS="TOKEN">?</SPAN> indicates a test for a condition.</P></DIV><P><ANAME="CSTRINARY"></A></P><P>In a <AHREF="dblparens.html">double parentheses construct</A>, the <SPANCLASS="TOKEN">?</SPAN> can serve as an element of a C-style <ICLASS="FIRSTTERM">trinary</I> operator, <TTCLASS="VARNAME">?:</TT>. <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING"> 1 (( var0 = var1<98?9:21 )) 2 # ^ ^ 3 4 # if [ "$var1" -lt 98 ] 5 # then 6 # var0=9 7 # else 8 # var0=21 9 # fi</PRE></TD></TR></TABLE></P><P>In a <AHREF="parameter-substitution.html#PARAMSUBREF">parameter substitution</A> expression, the <SPANCLASS="TOKEN">?</SPAN> <AHREF="parameter-substitution.html#QERRMSG">tests whether a variable has been set</A>.</P></DD><DT><SPANCLASS="TOKEN">?</SPAN></DT><DD><DIVCLASS="FORMALPARA"><P><B>wild card. </B><ANAME="QUEXWC"></A>The <SPANCLASS="TOKEN">?</SPAN> character serves as a single-character <SPANCLASS="QUOTE">"wild card"</SPAN> for filename expansion in <AHREF="globbingref.html">globbing</A>, as well as <AHREF="regexp.html#QUEXREGEX">representing one character</A> in an <AHREF="regexp.html#EXTREGEX">extended regular expression</A>.</P></DIV></DD><DT><SPANCLASS="TOKEN">$</SPAN></DT><DD><DIVCLASS="FORMALPARA"><P><B><AHREF="variables.html#VARSUBN">Variable substitution</A> (contents of a variable). </B> <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING"> 1 var1=5 2 var2=23skidoo 3 4 echo $var1 # 5 5 echo $var2 # 23skidoo</PRE></TD></TR></TABLE> </P></DIV><P><ANAME="VARPREFIXREF"></A></P><P>A <SPANCLASS="TOKEN">$</SPAN> prefixing a variable name indicates the <ICLASS="FIRSTTERM">value</I> the variable holds.</P></DD><DT><SPANCLASS="TOKEN">$</SPAN></DT><DD><DIVCLASS="FORMALPARA"><P><B>end-of-line. </B>In a <AHREF="regexp.html#REGEXREF">regular expression</A>, a <SPANCLASS="QUOTE">"$"</SPAN> addresses the end of a line of text.</P></DIV></DD><DT><SPANCLASS="TOKEN">${}</SPAN></DT><DD><DIVCLASS="FORMALPARA"><P><B><AHREF="parameter-substitution.html#PARAMSUBREF">Parameter substitution</A>. </B></P></DIV></DD><DT><SPANCLASS="TOKEN">$*</SPAN>, <SPANCLASS="TOKEN">$@</SPAN></DT><DD><DIVCLASS="FORMALPARA"><P><B><AHREF="variables2.html#APPREF">positional parameters</A>. </B></P></DIV></DD><DT><SPANCLASS="TOKEN">$?</SPAN></DT><DD><DIVCLASS="FORMALPARA"><P><B>exit status variable. </B>The <AHREF="exit-status.html#EXSREF">$? variable</A> holds the <AHREF="exit-status.html#EXITSTATUSREF">exit status</A> of a command, a <AHREF="functions.html#FUNCTIONREF">function</A>, or of the script itself.</P></DIV></DD><DT><ANAME="PROCESSIDREF"></A><SPANCLASS="TOKEN">$$</SPAN></DT><DD><DIVCLASS="FORMALPARA"><P><B>process ID variable. </B>The <AHREF="variables2.html#PROCCID">$$ variable</A> holds the <ICLASS="FIRSTTERM">process ID</I> <ANAME="AEN949"HREF="#FTN.AEN949">[2]</A> of the script in which it appears.</P></DIV></DD><DT><ANAME="PARENSREF"></A><SPANCLASS="TOKEN">()</SPAN></DT><DD><DIVCLASS="FORMALPARA"><P><B>command group. </B><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING"> 1 (a=hello; echo $a)</PRE></TD></TR></TABLE></P></DIV><DIVCLASS="IMPORTANT"><TABLECLASS="IMPORTANT"WIDTH="90%"BORDER="0"><TR><TDWIDTH="25"ALIGN="CENTER"VALIGN="TOP"><IMGSRC="common/important.png"HSPACE="5"ALT="Important"></TD><TDALIGN="LEFT"VALIGN="TOP"><P>A listing of commands within <TTCLASS="REPLACEABLE"><I>parentheses</I></TT> starts a <AHREF="subshells.html#SUBSHELLSREF">subshell</A>.</P><P>Variables inside parentheses, within the subshell, are not visible to the rest of the script. The parent process, the script, <AHREF="subshells.html#PARVIS">cannot read variables created in the child process</A>, the subshell. <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING"> 1 a=123 2 ( a=321; ) 3 4 echo "a = $a" # a = 123 5 # "a" within parentheses acts like a local variable.</PRE></TD
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -