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

📄 ch08_20.htm

📁 the unix power tools
💻 HTM
字号:
<HTML><!--Distributed by F --><HEAD><TITLE>[Chapter 8] 8.20 How Many Backslashes?</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:46Z"><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_19.htm"TITLE='8.19 "Special&quot; Characters and Operators '><LINKREL="next"HREF="ch09_01.htm"TITLE="9. Saving Time on the Command Line"></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_19.htm"TITLE='8.19 "Special&quot; Characters and Operators '><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT='Previous: 8.19 "Special&quot; Characters and Operators '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="CHAPTER"HREF="ch09_01.htm"TITLE="9. Saving Time on the Command Line"><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 9. Saving Time on the Command Line"BORDER="0"></A></TD></TR></TABLE>&nbsp;<HRALIGN="LEFT"WIDTH="515"TITLE="footer"></DIV><DIVCLASS="SECT1"><H2CLASS="sect1"><ACLASS="title"NAME="UPT-ART-0460">8.20 How Many Backslashes?</A></H2><PCLASS="para">The problem with backslashes is that many different programs use themas quoting characters. As a result, it's difficult to figure out howmany backslashes you need in any situation.<ACLASS="indexterm"NAME="AUTOID-9598"></A></P><PCLASS="para">Here's an example, taken from System V Release 4.(Notice that I'm using the standard System V version of <EMCLASS="emphasis">echo</EM> from<EMCLASS="emphasis">/bin/echo</EM>.SVR4 has four versions of <EMCLASS="emphasis">echo</EM>!)</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>/bin/echo hi \ there</B></CODE>hi  there% <CODECLASS="userinput"><B>/bin/echo hi \\ there</B></CODE>hi \ there% <CODECLASS="userinput"><B>/bin/echo hi \\\\ there</B></CODE>hi \ there</PRE></BLOCKQUOTE></P><PCLASS="para">In the first case, the shell uses the backslash to<SPANCLASS="link">quote (<ACLASS="linkend"HREF="ch08_14.htm"TITLE="Bourne Shell Quoting ">8.14</A>)</SPAN>the following space character.The space before the backslash is a word separator.So <EMCLASS="emphasis">echo</EM> gets two arguments: &quot;<CODECLASS="literal">hi</CODE>&quot; and &quot;<CODECLASS="literal"><IMGSRC="../chars/squ.gif"ALT=" ">there</CODE>&quot;(without the quotes)-where <CODECLASS="literal"><IMGSRC="../chars/squ.gif"ALT=" "></CODE> is the space characterthat was quoted by the backslash.As always, <EMCLASS="emphasis">echo</EM> prints a single space between each argument.The first space you see in the output is echo's argument-separatingspace, and the second space came along with the second argument(thanks to the backslash).</P><PCLASS="para">In the second case, the shell converts <CODECLASS="literal">\\</CODE> to <CODECLASS="literal">\</CODE>;the first backslash tells the shell to<SPANCLASS="link">quote (<ACLASS="linkend"HREF="ch08_14.htm"TITLE="Bourne Shell Quoting ">8.14</A>)</SPAN>(turn off the special meaning of) the second backslash.The <EMCLASS="emphasis">echo</EM> command gets three arguments,&quot;<CODECLASS="literal">hi</CODE>&quot;, &quot;<CODECLASS="literal">\</CODE>&quot; and &quot;<CODECLASS="literal">there</CODE>&quot;, and itechoes those arguments with a single space between each.(I've heard claimsthat, on some systems, this command wouldn't print any backslashes,but I wasn't able to reconstruct that situation.)</P><PCLASS="para">In the third case, the shell converts each pair of backslashes into abackslash, and runs the command <CODECLASS="literal">echo&nbsp;hi&nbsp;\\&nbsp;there</CODE>.But this isSystem V, and System V's <EMCLASS="emphasis">echo</EM><SPANCLASS="link">interprets backslashes (<ACLASS="linkend"HREF="ch08_06.htm"TITLE="Output Command-Line Arguments ">8.6</A>)</SPAN>as special characters.So when <EMCLASS="emphasis">echo</EM> sees the remaining two backslashes, it convertsthem into a single backslash. So you only see a single backslash,even though you typed four. On BSD systems, <EMCLASS="emphasis">echo</EM> doesn't dothis; you'd see two backslashes.For that matter, if you're using SVR4's C shell, with its built-in <EMCLASS="emphasis">echo</EM> command, you'll see the BSD behavior.You'll also see the BSD behavior if you're using SVR4's <EMCLASS="emphasis">/usr/ucb/echo</EM>.</P><PCLASS="para">The<SPANCLASS="link">terminal driver (<ACLASS="linkend"HREF="ch42_01.htm#UPT-ART-1142"TITLE="Making Sense Out of the Terminal Mess ">42.1</A>)</SPAN>is also capable of &quot;eating&quot; backslashes if theyappear before special characters. If abackslash precedes the &quot;erase&quot; character (normally CTRL-h) or the &quot;kill&quot; character (normallyCTRL-u), the terminal driver will pass the controlcharacter to the shell, rather than interpretingit as an editingcharacter. In the process, it &quot;eats&quot; the backslash. So if you type:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>echo \</B></CODE><KBDCLASS="keycap">[CTRL-u]</KBD></PRE></BLOCKQUOTE></P><PCLASS="para">The shell receives the line <CODECLASS="literal">echo&nbsp;CTRL-u</CODE>.See the <EMCLASS="emphasis">termio</EM> manual page for more information; there arecertainly system-dependent variations.</P><PCLASS="para">What's the point of this article? Well, backslashes are messy. Theshell, the terminal driver, <EMCLASS="emphasis">echo</EM> (sometimes), and several otherutilities use them. If you think very carefully, you can figure outexactly what's consuming them. If you're not of a rigorous frame ofmind, you can just add backslashes until you get what you want.(But, obviously, the non-rigorous approach has pitfalls.)I've seen situations in<SPANCLASS="link"><EMCLASS="emphasis">troff</EM> (<ACLASS="linkend"HREF="ch43_13.htm"TITLE="The Text Formatters nroff, troff, ditroff, ... ">43.13</A>)</SPAN>(which is another storyaltogether) where you need eight backslashes in order to have a singlebackslash left at the point where you want it!</P><PCLASS="para">(Extra credit:What happens when you put quotes (<CODECLASS="literal">&quot;</CODE> or <CODECLASS="literal">'</CODE>) around thestrings in the <EMCLASS="emphasis">echo</EM> commands above?Especially, should quotes affect the way that the <CODECLASS="literal">\</CODE><KBDCLASS="keycap">[CTRL-u]</KBD>is interpreted?)<ACLASS="indexterm"NAME="AUTOID-9652"></A><ACLASS="indexterm"NAME="AUTOID-9653"></A></P><DIVCLASS="sect1info"><PCLASS="SECT1INFO">- <SPANCLASS="authorinitials">ML</SPAN>, <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_19.htm"TITLE='8.19 "Special&quot; Characters and Operators '><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT='Previous: 8.19 "Special&quot; Characters and Operators '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="CHAPTER"HREF="ch09_01.htm"TITLE="9. Saving Time on the Command Line"><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 9. Saving Time on the Command Line"BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172">8.19 "Special&quot; Characters and Operators </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">9. Saving Time on the Command Line</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 &amp; 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 + -