⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch08_14.htm

📁 the unix power tools
💻 HTM
📖 第 1 页 / 共 2 页
字号:
<HTML><!--Distributed by F --><HEAD><TITLE>[Chapter 8] 8.14 Bourne Shell Quoting </TITLE><METANAME="DC.title"CONTENT="UNIX Power Tools"><METANAME="DC.creator"CONTENT="Jerry Peek, Tim O'Reilly &amp; Mike Loukides"><METANAME="DC.publisher"CONTENT="O'Reilly &amp; Associates, Inc."><METANAME="DC.date"CONTENT="1998-08-04T21:34:28Z"><METANAME="DC.type"CONTENT="Text.Monograph"><METANAME="DC.format"CONTENT="text/html"SCHEME="MIME"><METANAME="DC.source"CONTENT="1-56592-260-3"SCHEME="ISBN"><METANAME="DC.language"CONTENT="en-US"><METANAME="generator"CONTENT="Jade 1.1/O'Reilly DocBook 3.0 to HTML 4.0"><LINKREV="made"HREF="mailto:online-books@oreilly.com"TITLE="Online Books Comments"><LINKREL="up"HREF="ch08_01.htm"TITLE="8. How the Shell Interprets What You Type"><LINKREL="prev"HREF="ch08_13.htm"TITLE='8.13 Is It "2&gt;&amp;1 file&quot; or "&gt; file 2&gt;&amp;1&quot;? Why?'><LINKREL="next"HREF="ch08_15.htm"TITLE="8.15 Differences Between Bourne and C Shell Quoting "></HEAD><BODYBGCOLOR="#FFFFFF"TEXT="#000000"><DIVCLASS="htmlnav"><H1><IMGSRC="gifs/smbanner.gif"ALT="UNIX Power Tools"USEMAP="#srchmap"BORDER="0"></H1><MAPNAME="srchmap"><AREASHAPE="RECT"COORDS="0,0,466,58"HREF="index.htm"ALT="UNIX Power Tools"><AREASHAPE="RECT"COORDS="467,0,514,18"HREF="jobjects/fsearch.htm"ALT="Search this book"></MAP><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&gt;&amp;1 file&quot; or "&gt; file 2&gt;&amp;1&quot;? Why?'><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT='Previous: 8.13 Is It "2&gt;&amp;1 file&quot; or "&gt; file 2&gt;&amp;1&quot;? Why?'BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="171"><B><FONTFACE="ARIEL,HELVETICA,HELV,SANSERIF"SIZE="-1">Chapter 8<BR>How the Shell Interprets What You Type</FONT></B></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></TABLE>&nbsp;<HRALIGN="LEFT"WIDTH="515"TITLE="footer"></DIV><DIVCLASS="SECT1"><H2CLASS="sect1"><ACLASS="title"NAME="UPT-ART-0012">8.14 Bourne Shell Quoting </A></H2><PCLASS="para"><ACLASS="indexterm"NAME="UPT-ART-12-IX-SH-BOURNE-SHELL-QUOTING"></A><ACLASS="indexterm"NAME="UPT-ART-12-IX-QUOTING-IN-BOURNE-SHELL-SH-BOURNE"></A>I can't understand why some people see Bourne shell quoting as a scary,mysterious set of many rules.Bourne shell quoting is simple.(C shell quoting is slightly more complicated.See article<ACLASS="xref"HREF="ch08_15.htm"TITLE="Differences Between Bourne and C Shell Quoting ">8.15</A>.)</P><PCLASS="para">The overall idea is: <EMCLASS="emphasis">quoting turns off (disables) the special meaning ofcharacters</EM>.There are three quoting characters: a single quote (<CODECLASS="literal">'</CODE>),a double quote (<CODECLASS="literal">&quot;</CODE>), and a backslash (<CODECLASS="literal">\</CODE>).Note that a backquote (<CODECLASS="literal">`</CODE>) is <EMCLASS="emphasis">not</EM> a quoting character&nbsp;- it does<SPANCLASS="link">command substitution (<ACLASS="linkend"HREF="ch09_16.htm"TITLE="Command Substitution ">9.16</A>)</SPAN>.</P><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="UPT-ART-12-SECT-1.1">8.14.1 Special Characters </A></H3><PCLASS="para"><ACLASS="indexterm"NAME="UPT-ART-12-IX-SPECIAL-CHARACTERS-IN-BOURNE-SHELL-SH-BOURNE-SHELL"></A>Below are the characters that are special to the Bourne shell.You've probably already used some of them.Quoting these characters turns off their special meaning.(Yes, the last three characters are quote marks.You can quote quote marks; more on that later.)</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen"># &amp; * ? [ ] ( ) = | ^ ; &lt; &gt; ` $ &quot; ' \</PRE></BLOCKQUOTE></P><PCLASS="para">Space, tab, and newline also have special meaning: as argument separators.A slash (<CODECLASS="literal">/</CODE>) has special meaning to UNIX itself, but not theshell&nbsp;- so quoting doesn't change the meaning of slashes.</P></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="UPT-ART-12-SECT-1.2">8.14.2 How Quoting Works </A></H3><PCLASS="para"><ACLASS="xref"HREF="ch08_14.htm#UPT-ART-12-TAB-0"TITLE="Bourne Shell Quoting Characters">Table 8.1</A>summarizes the rules; you might want to look back at it while you read theexamples.</P><TABLECLASS="table"><CAPTIONCLASS="table"><ACLASS="title"NAME="UPT-ART-12-TAB-0">Table 8.1: Bourne Shell Quoting Characters</A></CAPTION><THEADCLASS="thead"><TRCLASS="row"VALIGN="TOP"><THCLASS="entry"ALIGN="LEFT"ROWSPAN="1"COLSPAN="1">Quoting Character</TH><THCLASS="entry"ALIGN="LEFT"ROWSPAN="1"COLSPAN="1">Explanation</TH></TR></THEAD><TBODYCLASS="tbody"><TRCLASS="row"VALIGN="TOP"><TDCLASS="entry"ROWSPAN="1"COLSPAN="1">'<CODECLASS="replaceable"><I>xxx</I></CODE>'</TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1"><PCLASS="para">Disable all special characters in <CODECLASS="replaceable"><I>xxx</I></CODE>.</P></TD></TR><TRCLASS="row"VALIGN="TOP"><TDCLASS="entry"ROWSPAN="1"COLSPAN="1">&quot;<CODECLASS="replaceable"><I>xxx</I></CODE>&quot;</TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1"><PCLASS="para">Disable all special characters in <CODECLASS="replaceable"><I>xxx</I></CODE> except <CODECLASS="literal">$</CODE>,<CODECLASS="literal">`</CODE>, and <CODECLASS="literal">\</CODE>.</P></TD></TR><TRCLASS="row"VALIGN="TOP"><TDCLASS="entry"ROWSPAN="1"COLSPAN="1">\<CODECLASS="replaceable"><I>x</I></CODE></TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1"><PCLASS="para">Disable special meaning of character <CODECLASS="replaceable"><I>x</I></CODE>.At end of line, a <CODECLASS="literal">\</CODE> removes the newline character (continues line).</P></TD></TR></TBODY></TABLE><PCLASS="para">To understand which characters will be quoted, imagine this:the Bourne shell reads what you type at a prompt, or the lines in a shellscript, character by character from first to last.(It's actually more complicated than that, but not for the purposes of quoting.)</P><PCLASS="para">When the shell reads one of the three quoting characters, it:</P><ULCLASS="itemizedlist"><LICLASS="listitem"><PCLASS="para">Strips away that quoting character.</P></LI><LICLASS="listitem"><PCLASS="para">Turns off (disables) special meaning of some or all other character(s) until theend of the quoted section, by the rules in<ACLASS="xref"HREF="ch08_14.htm#UPT-ART-12-TAB-0"TITLE="Bourne Shell Quoting Characters">Table 8.1</A>.</P></LI></UL><PCLASS="para">You also need to know how many characters will be quoted.The next few sections have examples to demonstrate those rules.Try typing the examples at a Bourne shell prompt, if you'd like.(Don't use C shell;<SPANCLASS="link">it's different (<ACLASS="linkend"HREF="ch08_15.htm"TITLE="Differences Between Bourne and C Shell Quoting ">8.15</A>)</SPAN>.)If you need to start a Bourne-type shell, type <EMCLASS="emphasis">sh</EM>; use CTRL-dwhen you're done.</P><ULCLASS="itemizedlist"><LICLASS="listitem"><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-8459"></A>A <BCLASS="emphasis.bold">backslash</B> (<CODECLASS="literal">\</CODE>) turns off special meaning (if any)of the next character.For example, <CODECLASS="literal">\*</CODE> is a literal asterisk, not a filename wildcard.So, the first<SPANCLASS="link"><EMCLASS="emphasis">expr</EM> (<ACLASS="linkend"HREF="ch45_28.htm"TITLE="Quick Reference: expr ">45.28</A>)</SPAN>command gets the three arguments<CODECLASS="literal">79&nbsp;*&nbsp;45</CODE> and multiplies those two numbers:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">$ <CODECLASS="userinput"><B>expr 79 \* 45</B></CODE>3555$ <CODECLASS="userinput"><B>expr 79 * 45</B></CODE>expr: syntax error</PRE></BLOCKQUOTE></P><PCLASS="para">In the second example, without the backslash, the shell expanded <CODECLASS="literal">*</CODE>into a list of filenames&nbsp;- which confused <EMCLASS="emphasis">expr</EM>.(If you want to see what I mean, repeat those two examples using<SPANCLASS="link"><EMCLASS="emphasis">echo</EM> (<ACLASS="linkend"HREF="ch08_06.htm"TITLE="Output Command-Line Arguments ">8.6</A>)</SPAN>instead of <EMCLASS="emphasis">expr</EM>.)</P></LI><LICLASS="listitem"><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-8480"></A><ACLASS="indexterm"NAME="AUTOID-8483"></A><ACLASS="indexterm"NAME="AUTOID-8486"></A><ACLASS="indexterm"NAME="AUTOID-8489"></A><ACLASS="indexterm"NAME="AUTOID-8492"></A><ACLASS="indexterm"NAME="AUTOID-8495"></A><ACLASS="indexterm"NAME="AUTOID-8498"></A>A <BCLASS="emphasis.bold">single quote</B> (<CODECLASS="literal">'</CODE>) turns off special meaning of allcharacters until the next single quote is found.So, in the command line below, the words between the twosingle quotes are quoted.The quotes themselves are removed by the shell.Although this mess is probably not what you want, it's a gooddemonstration of what quoting does:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">$ <CODECLASS="userinput"><B>echo Hey!       What's next?  Mike's #1 friend has $$.</B></CODE>Hey! Whats next?  Mikes</PRE></BLOCKQUOTE></P><PCLASS="para">Let's take a close look at what happened.Spaces outside the quotes are treated as argument separators; the shellignores the multiple spaces.As article<ACLASS="xref"HREF="ch08_06.htm"TITLE="Output Command-Line Arguments ">8.6</A>explains, <EMCLASS="emphasis">echo</EM> prints a single space between each argument it gets.Spaces inside the quotes are passed on to <EMCLASS="emphasis">echo</EM> literally.The question mark (<CODECLASS="literal">?</CODE>) is quoted; it's given to <EMCLASS="emphasis">echo</EM> as is,not used as a wildcard.</P><PCLASS="para">So, <EMCLASS="emphasis">echo</EM> printed its first argument <CODECLASS="literal">Hey!</CODE> and a single space.The second argument to <EMCLASS="emphasis">echo</EM> is <CODECLASS="literal">Whats&nbsp;next?&nbsp;&nbsp;Mikes</CODE>;it's all a single argument because the single quotes surrounded the spaces(notice that <EMCLASS="emphasis">echo</EM> prints the two spaces after the question mark:<CODECLASS="literal">?&nbsp;&nbsp;</CODE>).The next argument, <CODECLASS="literal">#1</CODE>, starts with a hash mark, which is a<SPANCLASS="link">comment character (<ACLASS="linkend"HREF="ch44_02.htm"TITLE="Writing a Simple Shell Program ">44.2</A>)</SPAN>.That means the shell will ignore the rest of the string; it isn't passedto <EMCLASS="emphasis">echo</EM>.</P></LI><LICLASS="listitem"><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-8524"></A><BCLASS="emphasis.bold">Double quotes</B> (<CODECLASS="literal">&quot;</CODE>) work almost like single quotes.The difference is that double quoting allows the characters <CODECLASS="literal">$</CODE>(dollar sign), <CODECLASS="literal">`</CODE> (backquote), and <CODECLASS="literal">\</CODE> (backslash)to keep their special meanings.That lets you do<SPANCLASS="link">variable substitution (<ACLASS="linkend"HREF="ch06_08.htm"TITLE="Shell Variables "

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -