📄 quoting.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><HTML><HEAD><TITLE>Quoting</TITLE><METANAME="GENERATOR"CONTENT="Modular DocBook HTML Stylesheet Version 1.76b+"><LINKREL="HOME"TITLE="Advanced Bash-Scripting Guide"HREF="index.html"><LINKREL="UP"TITLE="Basics"HREF="part2.html"><LINKREL="PREVIOUS"TITLE="Special Variable Types"HREF="othertypesv.html"><LINKREL="NEXT"TITLE="Escaping"HREF="escapingsection.html"><METAHTTP-EQUIV="Content-Style-Type"CONTENT="text/css"><LINKREL="stylesheet"HREF="common/kde-common.css"TYPE="text/css"><METAHTTP-EQUIV="Content-Type"CONTENT="text/html; charset=iso-8859-1"><METAHTTP-EQUIV="Content-Language"CONTENT="en"><LINKREL="stylesheet"HREF="common/kde-localised.css"TYPE="text/css"TITLE="KDE-English"><LINKREL="stylesheet"HREF="common/kde-default.css"TYPE="text/css"TITLE="KDE-Default"></HEAD><BODYCLASS="CHAPTER"BGCOLOR="#FFFFFF"TEXT="#000000"LINK="#AA0000"VLINK="#AA0055"ALINK="#AA0000"STYLE="font-family: sans-serif;"><DIVCLASS="NAVHEADER"><TABLESUMMARY="Header navigation table"WIDTH="100%"BORDER="0"CELLPADDING="0"CELLSPACING="0"><TR><THCOLSPAN="3"ALIGN="center">Advanced Bash-Scripting Guide: An in-depth exploration of the art of shell scripting</TH></TR><TR><TDWIDTH="10%"ALIGN="left"VALIGN="bottom"><AHREF="othertypesv.html"ACCESSKEY="P">Prev</A></TD><TDWIDTH="80%"ALIGN="center"VALIGN="bottom"></TD><TDWIDTH="10%"ALIGN="right"VALIGN="bottom"><AHREF="escapingsection.html"ACCESSKEY="N">Next</A></TD></TR></TABLE><HRALIGN="LEFT"WIDTH="100%"></DIV><DIVCLASS="CHAPTER"><H1><ANAME="QUOTING"></A>Chapter 5. Quoting</H1><P><ANAME="QUOTINGREF"></A></P><P>Quoting means just that, bracketing a string in quotes. This has the effect of protecting <AHREF="special-chars.html#SCHARLIST1">special characters</A> in the string from reinterpretation or expansion by the shell or shell script. (A character is <SPANCLASS="QUOTE">"special"</SPAN> if it has an interpretation other than its literal meaning, such as the <SPANCLASS="TOKEN">wild card</SPAN> character -- <SPANCLASS="TOKEN">*</SPAN>.)</P><P> <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="SCREEN"> <TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>ls -l [Vv]*</B></TT> <TTCLASS="COMPUTEROUTPUT">-rw-rw-r-- 1 bozo bozo 324 Apr 2 15:05 VIEWDATA.BAT -rw-rw-r-- 1 bozo bozo 507 May 4 14:25 vartrace.sh -rw-rw-r-- 1 bozo bozo 539 Apr 14 17:11 viewdata.sh</TT> <TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>ls -l '[Vv]*'</B></TT> <TTCLASS="COMPUTEROUTPUT">ls: [Vv]*: No such file or directory</TT></PRE></TD></TR></TABLE> </P><P><ANAME="QUOTINGDEF"></A></P><TABLECLASS="SIDEBAR"BORDER="1"CELLPADDING="5"><TR><TD><DIVCLASS="SIDEBAR"><ANAME="AEN2305"></A><P>In everyday speech or writing, when we <SPANCLASS="QUOTE">"quote"</SPAN> a phrase, we set it apart and give it special meaning. In a Bash script, when we <ICLASS="FIRSTTERM">quote</I> a string, we set it apart and protect its <ICLASS="FIRSTTERM">literal</I> meaning.</P></DIV></TD></TR></TABLE><P>Certain programs and utilities reinterpret or expand special characters in a quoted string. An important use of quoting is protecting a command-line parameter from the shell, but still letting the calling program expand it.</P><P> <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="SCREEN"> <TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>grep '[Ff]irst' *.txt</B></TT> <TTCLASS="COMPUTEROUTPUT">file1.txt:This is the first line of file1.txt. file2.txt:This is the First line of file2.txt.</TT></PRE></TD></TR></TABLE> </P><P>Note that the unquoted <TTCLASS="USERINPUT"><B>grep [Ff]irst *.txt</B></TT> works under the Bash shell. <ANAME="AEN2318"HREF="#FTN.AEN2318">[1]</A> </P><P>Quoting can also suppress <AHREF="internal.html#ECHOREF">echo's</A> <SPANCLASS="QUOTE">"appetite"</SPAN> for newlines.</P><P> <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="SCREEN"> <TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>echo $(ls -l)</B></TT> <TTCLASS="COMPUTEROUTPUT">total 8 -rw-rw-r-- 1 bo bo 13 Aug 21 12:57 t.sh -rw-rw-r-- 1 bo bo 78 Aug 21 12:57 u.sh</TT> <TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>echo "$(ls -l)"</B></TT> <TTCLASS="COMPUTEROUTPUT">total 8 -rw-rw-r-- 1 bo bo 13 Aug 21 12:57 t.sh -rw-rw-r-- 1 bo bo 78 Aug 21 12:57 u.sh</TT></PRE></TD></TR></TABLE> </P><DIVCLASS="SECT1"><H1CLASS="SECT1"><ANAME="QUOTINGVAR"></A>5.1. Quoting Variables</H1><P>When referencing a variable, it is generally advisable to enclose its name in double quotes. This prevents reinterpretation of all special characters within the quoted string -- the variable name <ANAME="AEN2336"HREF="#FTN.AEN2336">[2]</A> -- except <SPANCLASS="TOKEN">$</SPAN>, <SPANCLASS="TOKEN">`</SPAN> (backquote), and <SPANCLASS="TOKEN">\</SPAN> (escape). <ANAME="AEN2342"HREF="#FTN.AEN2342">[3]</A> Keeping <SPANCLASS="TOKEN">$</SPAN> as a special character within double quotes permits referencing a quoted variable (<TTCLASS="REPLACEABLE"><I>"$variable"</I></TT>), that is, replacing the variable with its value (see <AHREF="variables.html#EX9">Example 4-1</A>, above).</P><P><ANAME="WSQUO"></A></P><P>Use double quotes to prevent word splitting. <ANAME="AEN2378"HREF="#FTN.AEN2378">[4]</A> An argument enclosed in double quotes presents itself as a single word, even if it contains <AHREF="special-chars.html#WHITESPACEREF">whitespace</A> separators. <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING"> 1 variable1="a variable containing five words" 2 COMMAND This is $variable1 # Executes COMMAND with 7 arguments: 3 # "This" "is" "a" "variable" "containing" "five" "words" 4 5 COMMAND "This is $variable1" # Executes COMMAND with 1 argument: 6 # "This is a variable containing five words" 7 8 9 variable2="" # Empty. 10 11 COMMAND $variable2 $variable2 $variable2 12 # Executes COMMAND with no arguments. 13 COMMAND "$variable2" "$variable2" "$variable2" 14 # Executes COMMAND with 3 empty arguments. 15 COMMAND "$variable2 $variable2 $variable2" 16 # Executes COMMAND with 1 argument (2 spaces). 17 18 # Thanks, St閜hane Chazelas.</PRE></TD></TR></TABLE></P><DIVCLASS="TIP"><TABLECLASS="TIP"WIDTH="100%"BORDER="0"><TR><TDWIDTH="25"ALIGN="CENTER"VALIGN="TOP"><IMGSRC="common/tip.png"HSPACE="5"ALT="Tip"></TD><TDALIGN="LEFT"VALIGN="TOP"><P>Enclosing the arguments to an <BCLASS="COMMAND">echo</B> statement in double quotes is necessary only when word splitting or preservation of <AHREF="special-chars.html#WHITESPACEREF">whitespace</A> is an issue.</P></TD></TR></TABLE></DIV><DIVCLASS="EXAMPLE"><HR><ANAME="WEIRDVARS"></A><P><B>Example 5-1. Echoing Weird Variables</B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING"> 1 #!/bin/bash 2 # weirdvars.sh: Echoing weird variables. 3 4 var="'(]\\{}\$\"" 5 echo $var # '(]\{}$" 6 echo "$var" # '(]\{}$" Doesn't make a difference. 7 8 echo 9 10 IFS='\' 11 echo $var # '(] {}$" \ converted to space. Why? 12 echo "$var" # '(]\{}$" 13 14 # Examples above supplied by Stephane Chazelas. 15 16 exit 0</PRE></TD></TR></TABLE><HR></DIV><P>Single quotes (<SPANCLASS="TOKEN">' '</SPAN>) operate similarly to double quotes, but do not permit referencing variables, since the special meaning of <SPANCLASS="TOKEN">$</SPAN> is turned off. Within single quotes, <SPANCLASS="emphasis"><ICLASS="EMPHASIS">every</I></SPAN> special character except <SPANCLASS="TOKEN">'</SPAN> gets interpreted literally. Consider single quotes (<SPANCLASS="QUOTE">"full quoting"</SPAN>) to be a stricter method of quoting than double quotes (<SPANCLASS="QUOTE">"partial quoting"</SPAN>).</P><DIVCLASS="NOTE"><TABLECLASS="NOTE"WIDTH="100%"BORDER="0"><TR><TDWIDTH="25"ALIGN="CENTER"VALIGN="TOP"><IMGSRC="common/note.png"HSPACE="5"ALT="Note"></TD><TDALIGN="LEFT"VALIGN="TOP"><P>Since even the escape character (<SPANCLASS="TOKEN">\</SPAN>) gets a literal interpretation within single quotes, trying to enclose a single quote within single quotes will not yield the expected result. <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING"> 1 echo "Why can't I write 's between single quotes" 2 3 echo 4 5 # The roundabout method. 6 echo 'Why can'\''t I write '"'"'s between single quotes' 7 # |-------| |----------| |-----------------------| 8 # Three single-quoted strings, with escaped and quoted single quotes between. 9 10 # This example courtesy of St閜hane Chazelas.</PRE></TD></TR></TABLE> </P></TD></TR></TABLE></DIV></DIV></DIV><H3CLASS="FOOTNOTES">Notes</H3><TABLEBORDER="0"CLASS="FOOTNOTES"WIDTH="100%"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="5%"><ANAME="FTN.AEN2318"HREF="quoting.html#AEN2318">[1]</A></TD><TDALIGN="LEFT"VALIGN="TOP"WIDTH="95%"><P>Unless there is a file named <TTCLASS="FILENAME">first</TT> in the current working directory. Yet another reason to <ICLASS="FIRSTTERM">quote</I>. (Thank you, Harald Koenig, for pointing this out.</P></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="5%"><ANAME="FTN.AEN2336"HREF="quoting.html#AEN2336">[2]</A></TD><TDALIGN="LEFT"VALIGN="TOP"WIDTH="95%"><P>It also has side-effects on the <ICLASS="FIRSTTERM">value</I> of the variable (see below)</P></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="5%"><ANAME="FTN.AEN2342"HREF="quoting.html#AEN2342">[3]</A></TD><TDALIGN="LEFT"VALIGN="TOP"WIDTH="95%"><P>Encapsulating <SPANCLASS="QUOTE">"!"</SPAN> within double quotes gives an error when used <SPANCLASS="emphasis"><ICLASS="EMPHASIS">from the command line</I></SPAN>. This is interpreted as a <AHREF="histcommands.html">history command</A>. Within a script, though, this problem does not occur, since the Bash history mechanism is disabled then.</P><P>Of more concern is the <SPANCLASS="emphasis"><ICLASS="EMPHASIS">apparently</I></SPAN> inconsistent behavior of <SPANCLASS="QUOTE">"\"</SPAN> within double quotes.</P><P> <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="SCREEN"> <TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>echo hello\!</B></TT> <TTCLASS="COMPUTEROUTPUT">hello!</TT> <TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>echo "hello\!"</B></TT> <TTCLASS="COMPUTEROUTPUT">hello\!</TT> <TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>echo -e x\ty</B></TT> <TTCLASS="COMPUTEROUTPUT">xty</TT> <TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>echo -e "x\ty"</B></TT> <TTCLASS="COMPUTEROUTPUT">x y</TT> </PRE></TD></TR></TABLE> </P><P>What happens is that double quotes normally <SPANCLASS="emphasis"><ICLASS="EMPHASIS">escape</I></SPAN> the <SPANCLASS="QUOTE">"\"</SPAN> escape character, so that it echoes literally. However, the <TTCLASS="OPTION">-e</TT> option to <ICLASS="FIRSTTERM">echo</I> changes that. It causes the <SPANCLASS="QUOTE">"\t"</SPAN> to be interpreted as a <ICLASS="FIRSTTERM">tab</I>.</P><P>(Thank you, Wayne Pollock, for pointing this out, and Geoff Lee for explaining it.) </P></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="5%"><ANAME="FTN.AEN2378"HREF="quoting.html#AEN2378">[4]</A></TD><TDALIGN="LEFT"VALIGN="TOP"WIDTH="95%"><P><SPANCLASS="QUOTE">"Word splitting"</SPAN>, in this context, means dividing a character string into a number of separate and discrete arguments.</P></TD></TR></TABLE><DIVCLASS="NAVFOOTER"><HRALIGN="LEFT"WIDTH="100%"><TABLESUMMARY="Footer navigation table"WIDTH="100%"BORDER="0"CELLPADDING="0"CELLSPACING="0"><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top"><AHREF="othertypesv.html"ACCESSKEY="P">Prev</A></TD><TDWIDTH="34%"ALIGN="center"VALIGN="top"><AHREF="index.html"ACCESSKEY="H">Home</A></TD><TDWIDTH="33%"ALIGN="right"VALIGN="top"><AHREF="escapingsection.html"ACCESSKEY="N">Next</A></TD></TR><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top">Special Variable Types</TD><TDWIDTH="34%"ALIGN="center"VALIGN="top"><AHREF="part2.html"ACCESSKEY="U">Up</A></TD><TDWIDTH="33%"ALIGN="right"VALIGN="top">Escaping</TD></TR></TABLE></DIV></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -