📄 ch08_14.htm
字号:
>6.8</A>, <ACLASS="linkend"HREF="ch06_01.htm#UPT-ART-1170"TITLE="What Environment Variables Are Good For ">6.1</A>)</SPAN>and<SPANCLASS="link">command substitution (<ACLASS="linkend"HREF="ch09_16.htm"TITLE="Command Substitution ">9.16</A>)</SPAN>inside double quotes - and also to stop that substitution where you need to.</P><PCLASS="para"> For now, let's repeat the example above.This time, put double quotes around the single quotes (actually, aroundthe whole string):</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">$ <CODECLASS="userinput"><B>echo "Hey! What's next? Mike's #1 friend has $$."</B></CODE>Hey! What's next? Mike's #1 friend has 18437.</PRE></BLOCKQUOTE></P><PCLASS="para">The opening double quote isn't matched until the end of the string.So, all the spaces between the double quotes lose their special meaning - and theshell passes the whole string to <EMCLASS="emphasis">echo</EM> as one argument.The single quotes also lose their special meaning - because double quotesturn off the special meaning of single quotes!So, the single quotes aren't stripped off as they were in the previousexample; <EMCLASS="emphasis">echo</EM> prints them.</P><PCLASS="para">What else lost its special meaning?The hash mark (<CODECLASS="literal">#</CODE>) did; notice that the rest of the string waspassed to <EMCLASS="emphasis">echo</EM> this time - because it wasn't "commented out."<ACLASS="indexterm"NAME="AUTOID-8544"></A>But the dollar sign (<CODECLASS="literal">$</CODE>) didn't lose its meaning;the <CODECLASS="literal">$$</CODE> was expandedinto the shell's<SPANCLASS="link">process ID number (<ACLASS="linkend"HREF="ch38_03.htm"TITLE="Managing Processes: Overall Concepts ">38.3</A>)</SPAN>(in this shell, <CODECLASS="literal">18437</CODE>).</P></LI></UL><PCLASS="para">In the previous example, what would happen if you put the <CODECLASS="literal">$</CODE> insidethe single quotes?(Single quotes turn off the meaning of <CODECLASS="literal">$</CODE>, remember.)Would the shell still expand <CODECLASS="literal">$$</CODE> to its value?Yes, it would: the single quotes have lost their special meaning,so they don't affect any characters between themselves:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">$ <CODECLASS="userinput"><B>echo "What's next? How many $$ did Mike's friend bring?"</B></CODE>What's next? How many 18437 did Mike's friend bring?</PRE></BLOCKQUOTE></P><PCLASS="para">How can you make both the <CODECLASS="literal">$$</CODE> and the single quotes print literally?The easiest way is with a backslash, which still works inside doublequotes:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">$ <CODECLASS="userinput"><B>echo "What's next? How many \$\$ did Mike's friend bring?"</B></CODE>What's next? How many $$ did Mike's friend bring?</PRE></BLOCKQUOTE></P><PCLASS="para">Here's another way to solve the problem.A careful look at this will show a lot about shell quoting:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">$ <CODECLASS="userinput"><B>echo "What's next? How many "'$$'" did Mike's friend bring?"</B></CODE>What's next? How many $$ did Mike's friend bring?</PRE></BLOCKQUOTE></P><PCLASS="para">To read that example, remember that a double quote quotes charactersuntil the next double quote is found.The same is true for single quotes.So, the string <CODECLASS="literal">What's next? How many </CODE>(including the space at the end) is inside a pair of double quotes.The <CODECLASS="literal">$$</CODE> is inside a pair of single quotes.The rest of the line is inside another pair of double quotes.Both of the double-quoted strings contain a single quote; the doublequotes turn off its special meaning and the single quote is printed literally.</P></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="UPT-ART-12-SECT-1.3">8.14.3 Single Quotes Inside Single Quotes? </A></H3><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-8573"></A>You can't put single quotes inside single quotes.A single quote turns off <EMCLASS="emphasis">all</EM> special meaning until the nextsingle quote.Use double quotes and backslashes.<ACLASS="indexterm"NAME="AUTOID-8576"></A></P></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="UPT-ART-12-SECT-1.4">8.14.4 Multiline Quoting </A></H3><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-8580"></A>Once you type a single quote or double quote, everything is quoted.The quoting can stretch across many lines.(The C shell doesn't work this way.)</P><PCLASS="para">For example, in the short script shown in<ACLASS="xref"HREF="ch08_14.htm#UPT-ART-12-FIG-0"TITLE="Matching Quotes">Figure 8.1</A>,you might think that the <CODECLASS="literal">$1</CODE> isinside quotes... but it isn't.</P><H4CLASS="figure"><ACLASS="title"NAME="UPT-ART-12-FIG-0">Figure 8.1: Matching Quotes</A></H4><IMGCLASS="graphic"SRC="figs/0012.gif"ALT="Figure 8.1"><PCLASS="para">Actually, everything <EMCLASS="emphasis">but</EM> <CODECLASS="literal">$1</CODE> is in quotes.The gray shaded area shows the quoted parts.So <CODECLASS="literal">$1</CODE> is expanded by the Bourne shell, and not by <EMCLASS="emphasis">awk</EM>.</P><PCLASS="para">Here's another example.Let's store a<SPANCLASS="link">shell variable (<ACLASS="linkend"HREF="ch06_08.htm"TITLE="Shell Variables ">6.8</A>)</SPAN>with a multiline message, the kind that might be used in a shell program.<ACLASS="indexterm"NAME="AUTOID-8596"></A>A shell variable must be stored as a single argument; any argumentseparators (spaces, etc.) must be quoted.Inside double quotes, <CODECLASS="literal">$</CODE> and <CODECLASS="literal">`</CODE> are interpreted(<EMCLASS="emphasis">before</EM> the variable is stored, by the way).The opening double quote isn't closed by the end of the first line;the Bourne shell prints<SPANCLASS="link">secondary prompts (<ACLASS="linkend"HREF="ch09_13.htm"TITLE="Multiline Commands, Secondary Prompts ">9.13</A>)</SPAN>(<CODECLASS="literal">></CODE>) until all quotesare closed:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">$ <CODECLASS="userinput"><B>greeting="Hi, $USER.> The date and time now> are: `date`."</B></CODE>$ <CODECLASS="userinput"><B>echo "$greeting"</B></CODE>Hi, jerry.The date and time noware: Tue Sep 1 13:48:12 EDT 1992.$ <CODECLASS="userinput"><B>echo $greeting</B></CODE>Hi, jerry. The date and time now are: Tue Sep 1 13:48:12 EDT 1992.$</PRE></BLOCKQUOTE></P><PCLASS="para">The first <EMCLASS="emphasis">echo</EM> command line uses double quotes.So, the shell variable is expanded,but the shell doesn't use the spaces and newlines in the variableas argument separators.(Look at the extra spaces after the word <CODECLASS="literal">are:</CODE>.)The second <EMCLASS="emphasis">echo</EM> doesn't use double quotes.The spaces and newlines are treated as argument separators; the shellpasses 14 arguments to <EMCLASS="emphasis">echo</EM>, which prints them with singlespaces between.</P><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-8615"></A>A backslash has a quirk you should know about.If you use it outside quotes, at the end of a line (just before thenewline), the newline will be <EMCLASS="emphasis">deleted</EM>.Inside single quotes, though, a backslash at the end of a line iscopiedas is.Here are examples.I've numbered the prompts (<CODECLASS="literal">1$</CODE>, <CODECLASS="literal">2$</CODE>, and so on):</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">1$ <CODECLASS="userinput"><B>echo "a long long long long long long</B></CODE>> <CODECLASS="userinput"><B>line or two"</B></CODE>a long long long long long longline or two2$ <CODECLASS="userinput"><B>echo a long long long long long long\</B></CODE>> <CODECLASS="userinput"><B>line</B></CODE>a long long long long long longline3$ <CODECLASS="userinput"><B>echo a long long long long long long \</B></CODE>> <CODECLASS="userinput"><B>line</B></CODE>a long long long long long long line4$ <CODECLASS="userinput"><B>echo "a long long long long long long\</B></CODE>> <CODECLASS="userinput"><B>line"</B></CODE>a long long long long long longline5$ <CODECLASS="userinput"><B>echo 'a long long long long long long\</B></CODE>> <CODECLASS="userinput"><B>line'</B></CODE>a long long long long long long\line</PRE></BLOCKQUOTE></P><PCLASS="para">You've seen an example like <BCLASS="emphasis.bold">example 1</B> before.The newline is in quotes, so it isn't an argument separator;<EMCLASS="emphasis">echo</EM> prints it with the rest of the (single two-line) argument.In <BCLASS="emphasis.bold">example 2</B>, the backslash before the newline tells the shell to deletethe newline; the words <CODECLASS="literal">long</CODE> and <CODECLASS="literal">line</CODE> are passed to<EMCLASS="emphasis">echo</EM> as one argument.<BCLASS="emphasis.bold">Example 3</B> is usually what you want when you're typing long lists ofcommand-line arguments: Type a space (an argument separator) before thebackslash and newline.In <BCLASS="emphasis.bold">example 4</B>, the backslash inside the double quotes is ignored(compare to example 1).Inside single quotes, as in <BCLASS="emphasis.bold">example 5</B>, the backslash has nospecial meaning; it's passed on to <EMCLASS="emphasis">echo</EM>.</P><ACLASS="indexterm"NAME="AUTOID-8644"></A><ACLASS="indexterm"NAME="AUTOID-8645"></A></DIV><DIVCLASS="sect1info"><PCLASS="SECT1INFO">- <SPANCLASS="authorinitials">JP</SPAN></P></DIV></DIV><DIVCLASS="htmlnav"><P></P><HRALIGN="LEFT"WIDTH="515"TITLE="footer"><TABLEWIDTH="515"BORDER="0"CELLSPACING="0"CELLPADDING="0"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172"><ACLASS="SECT1"HREF="ch08_13.htm"TITLE='8.13 Is It "2>&1 file" or "> file 2>&1"? Why?'><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT='Previous: 8.13 Is It "2>&1 file" or "> file 2>&1"? Why?'BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="171"><ACLASS="book"HREF="index.htm"TITLE="UNIX Power Tools"><IMGSRC="gifs/txthome.gif"SRC="gifs/txthome.gif"ALT="UNIX Power Tools"BORDER="0"></A></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="172"><ACLASS="SECT1"HREF="ch08_15.htm"TITLE="8.15 Differences Between Bourne and C Shell Quoting "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 8.15 Differences Between Bourne and C Shell Quoting "BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172">8.13 Is It "2>&1 file" or "> file 2>&1"? Why?</TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="171"><ACLASS="index"HREF="index/idx_0.htm"TITLE="Book Index"><IMGSRC="gifs/index.gif"SRC="gifs/index.gif"ALT="Book Index"BORDER="0"></A></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="172">8.15 Differences Between Bourne and C Shell Quoting </TD></TR></TABLE><HRALIGN="LEFT"WIDTH="515"TITLE="footer"><IMGSRC="gifs/smnavbar.gif"SRC="gifs/smnavbar.gif"USEMAP="#map"BORDER="0"ALT="The UNIX CD Bookshelf Navigation"><MAPNAME="map"><AREASHAPE="RECT"COORDS="0,0,73,21"HREF="../index.htm"ALT="The UNIX CD Bookshelf"><AREASHAPE="RECT"COORDS="74,0,163,21"HREF="index.htm"ALT="UNIX Power Tools"><AREASHAPE="RECT"COORDS="164,0,257,21"HREF="../unixnut/index.htm"ALT="UNIX in a Nutshell"><AREASHAPE="RECT"COORDS="258,0,321,21"HREF="../vi/index.htm"ALT="Learning the vi Editor"><AREASHAPE="RECT"COORDS="322,0,378,21"HREF="../sedawk/index.htm"ALT="sed & awk"><AREASHAPE="RECT"COORDS="379,0,438,21"HREF="../ksh/index.htm"ALT="Learning the Korn Shell"><AREASHAPE="RECT"COORDS="439,0,514,21"HREF="../lrnunix/index.htm"ALT="Learning the UNIX Operating System"></MAP></DIV></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -