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

📄 escapingsection.html

📁 Shall高级编程
💻 HTML
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><HTML><HEAD><TITLE>Escaping</TITLE><METANAME="GENERATOR"CONTENT="Modular DocBook HTML Stylesheet Version 1.76b+"><LINKREL="HOME"TITLE="Advanced Bash-Scripting Guide"HREF="index.html"><LINKREL="UP"TITLE="Quoting"HREF="quoting.html"><LINKREL="PREVIOUS"TITLE="Quoting"HREF="quoting.html"><LINKREL="NEXT"TITLE="Exit and Exit Status"HREF="exit-status.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="SECT1"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="quoting.html"ACCESSKEY="P">Prev</A></TD><TDWIDTH="80%"ALIGN="center"VALIGN="bottom">Chapter 5. Quoting</TD><TDWIDTH="10%"ALIGN="right"VALIGN="bottom"><AHREF="exit-status.html"ACCESSKEY="N">Next</A></TD></TR></TABLE><HRALIGN="LEFT"WIDTH="100%"></DIV><DIVCLASS="SECT1"><H1CLASS="SECT1"><ANAME="ESCAPINGSECTION"></A>5.2. Escaping</H1><P><ANAME="ESCP"></A><ICLASS="FIRSTTERM">Escaping</I> is a method	of quoting single characters. The <SPANCLASS="TOKEN">escape</SPAN>	(<SPANCLASS="TOKEN">\</SPAN>) preceding a character tells the shell to	interpret that character literally.</P><DIVCLASS="CAUTION"><TABLECLASS="CAUTION"WIDTH="100%"BORDER="0"><TR><TDWIDTH="25"ALIGN="CENTER"VALIGN="TOP"><IMGSRC="common/caution.png"HSPACE="5"ALT="Caution"></TD><TDALIGN="LEFT"VALIGN="TOP"><P>With certain commands and utilities, such as <AHREF="internal.html#ECHOREF">echo</A> and <AHREF="sedawk.html#SEDREF">sed</A>, escaping a character may have the	opposite effect - it can toggle on a special meaning for that	character.</P></TD></TR></TABLE></DIV><DIVCLASS="VARIABLELIST"><P><B><ANAME="SPM"></A>Special meanings of certain	escaped characters</B></P><DL><DT>used with <BCLASS="COMMAND">echo</B> and	<BCLASS="COMMAND">sed</B></DT><DD><P></P></DD><DT><SPANCLASS="TOKEN">\n</SPAN></DT><DD><P>means newline</P></DD><DT><SPANCLASS="TOKEN">\r</SPAN></DT><DD><P>means return</P></DD><DT><SPANCLASS="TOKEN">\t</SPAN></DT><DD><P>means tab</P></DD><DT><SPANCLASS="TOKEN">\v</SPAN></DT><DD><P> means vertical tab</P></DD><DT><SPANCLASS="TOKEN">\b</SPAN></DT><DD><P>means backspace</P></DD><DT><SPANCLASS="TOKEN">\a</SPAN></DT><DD><P>means <SPANCLASS="QUOTE">"alert"</SPAN> (beep or flash)</P></DD><DT><SPANCLASS="TOKEN">\0xx</SPAN></DT><DD><P>translates to the octal ASCII	      equivalent of <TTCLASS="REPLACEABLE"><I>0xx</I></TT></P><DIVCLASS="EXAMPLE"><HR><ANAME="ESCAPED"></A><P><B>Example 5-2. Escaped Characters</B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;#!/bin/bash   2&nbsp;# escaped.sh: escaped characters   3&nbsp;   4&nbsp;echo; echo   5&nbsp;   6&nbsp;# Escaping a newline.   7&nbsp;# ------------------   8&nbsp;   9&nbsp;echo ""  10&nbsp;  11&nbsp;echo "This will print  12&nbsp;as two lines."  13&nbsp;# This will print  14&nbsp;# as two lines.  15&nbsp;  16&nbsp;echo "This will print \  17&nbsp;as one line."  18&nbsp;# This will print as one line.  19&nbsp;  20&nbsp;echo; echo  21&nbsp;  22&nbsp;echo "============="  23&nbsp;  24&nbsp;  25&nbsp;echo "\v\v\v\v"      # Prints \v\v\v\v literally.  26&nbsp;# Use the -e option with 'echo' to print escaped characters.  27&nbsp;echo "============="  28&nbsp;echo "VERTICAL TABS"  29&nbsp;echo -e "\v\v\v\v"   # Prints 4 vertical tabs.  30&nbsp;echo "=============="  31&nbsp;  32&nbsp;echo "QUOTATION MARK"  33&nbsp;echo -e "\042"       # Prints " (quote, octal ASCII character 42).  34&nbsp;echo "=============="  35&nbsp;  36&nbsp;# The $'\X' construct makes the -e option unnecessary.  37&nbsp;echo; echo "NEWLINE AND BEEP"  38&nbsp;echo $'\n'           # Newline.  39&nbsp;echo $'\a'           # Alert (beep).  40&nbsp;  41&nbsp;echo "==============="  42&nbsp;echo "QUOTATION MARKS"  43&nbsp;# Version 2 and later of Bash permits using the $'\nnn' construct.  44&nbsp;# Note that in this case, '\nnn' is an octal value.  45&nbsp;echo $'\t \042 \t'   # Quote (") framed by tabs.  46&nbsp;  47&nbsp;# It also works with hexadecimal values, in an $'\xhhh' construct.  48&nbsp;echo $'\t \x22 \t'  # Quote (") framed by tabs.  49&nbsp;# Thank you, Greg Keraunen, for pointing this out.  50&nbsp;# Earlier Bash versions allowed '\x022'.  51&nbsp;echo "==============="  52&nbsp;echo  53&nbsp;  54&nbsp;  55&nbsp;  56&nbsp;  57&nbsp;  58&nbsp;# Assigning ASCII characters to a variable.  59&nbsp;# ----------------------------------------  60&nbsp;quote=$'\042'        # " assigned to a variable.  61&nbsp;echo "$quote This is a quoted string, $quote and this lies outside the quotes."  62&nbsp;  63&nbsp;echo  64&nbsp;  65&nbsp;# Concatenating ASCII chars in a variable.  66&nbsp;triple_underline=$'\137\137\137'  # 137 is octal ASCII code for '_'.  67&nbsp;echo "$triple_underline UNDERLINE $triple_underline"  68&nbsp;  69&nbsp;echo  70&nbsp;  71&nbsp;ABC=$'\101\102\103\010'           # 101, 102, 103 are octal A, B, C.  72&nbsp;echo $ABC  73&nbsp;  74&nbsp;echo; echo  75&nbsp;  76&nbsp;escape=$'\033'                    # 033 is octal for escape.  77&nbsp;echo "\"escape\" echoes as $escape"  78&nbsp;#                                   no visible output.  79&nbsp;  80&nbsp;echo; echo  81&nbsp;  82&nbsp;exit 0</PRE></TD></TR></TABLE><HR></DIV><P>See <AHREF="bash2.html#EX77">Example 34-1</A> for another example of the              <TTCLASS="USERINPUT"><B>$'   '</B></TT> string expansion              construct.</P></DD><DT><SPANCLASS="TOKEN">\"</SPAN></DT><DD><P> gives the quote its literal meaning</P><P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;echo "Hello"                  # Hello   2&nbsp;echo "\"Hello\", he said."    # "Hello", he said.</PRE></TD></TR></TABLE></P></DD><DT><SPANCLASS="TOKEN">\$</SPAN></DT><DD><P>gives the dollar sign its literal meaning	      (variable name following <SPANCLASS="TOKEN">\$</SPAN> will not be	      referenced)</P><P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;echo "\$variable01"  # results in $variable01</PRE></TD></TR></TABLE></P></DD><DT><SPANCLASS="TOKEN">\\</SPAN></DT><DD><P>gives the backslash its literal meaning</P><P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;echo "\\"  # Results in \   2&nbsp;   3&nbsp;# Whereas . . .   4&nbsp;   5&nbsp;echo "\"   # Invokes secondary prompt from the command line.   6&nbsp;           # In a script, gives an error message.</PRE></TD></TR></TABLE></P></DD></DL></DIV><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>The behavior of <SPANCLASS="TOKEN">\</SPAN> depends on whether	it is itself escaped, quoted, or appearing within <AHREF="commandsub.html#COMMANDSUBREF">command substitution</A> or a <AHREF="here-docs.html#HEREDOCREF">here document</A>.	<TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;                      #  Simple escaping and quoting   2&nbsp;echo \z               #  z   3&nbsp;echo \\z              # \z   4&nbsp;echo '\z'             # \z   5&nbsp;echo '\\z'            # \\z   6&nbsp;echo "\z"             # \z   7&nbsp;echo "\\z"            # \z   8&nbsp;   9&nbsp;                      #  Command substitution  10&nbsp;echo `echo \z`        #  z  11&nbsp;echo `echo \\z`       #  z  12&nbsp;echo `echo \\\z`      # \z  13&nbsp;echo `echo \\\\z`     # \z  14&nbsp;echo `echo \\\\\\z`   # \z  15&nbsp;echo `echo \\\\\\\z`  # \\z  16&nbsp;echo `echo "\z"`      # \z  17&nbsp;echo `echo "\\z"`     # \z  18&nbsp;  19&nbsp;                      # Here document  20&nbsp;cat &#60;&#60;EOF                21&nbsp;\z                        22&nbsp;EOF                   # \z  23&nbsp;  24&nbsp;cat &#60;&#60;EOF                25&nbsp;\\z                       26&nbsp;EOF                   # \z  27&nbsp;  28&nbsp;# These examples supplied by St閜hane Chazelas.</PRE></TD></TR></TABLE>      </P><P>Elements of a string assigned to a variable may be escaped, but        the escape character alone may not be assigned to a variable.	<TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;variable=\   2&nbsp;echo "$variable"   3&nbsp;# Will not work - gives an error message:   4&nbsp;# test.sh: : command not found   5&nbsp;# A "naked" escape cannot safely be assigned to a variable.   6&nbsp;#   7&nbsp;#  What actually happens here is that the "\" escapes the newline and   8&nbsp;#+ the effect is        variable=echo "$variable"   9&nbsp;#+                      invalid variable assignment  10&nbsp;  11&nbsp;variable=\  12&nbsp;23skidoo  13&nbsp;echo "$variable"        #  23skidoo  14&nbsp;                        #  This works, since the second line  15&nbsp;                        #+ is a valid variable assignment.  16&nbsp;  17&nbsp;variable=\   18&nbsp;#        \^    escape followed by space  19&nbsp;echo "$variable"        # space  20&nbsp;  21&nbsp;variable=\\  22&nbsp;echo "$variable"        # \  23&nbsp;  24&nbsp;variable=\\\  25&nbsp;echo "$variable"  26&nbsp;# Will not work - gives an error message:  27&nbsp;# test.sh: \: command not found  28&nbsp;#  29&nbsp;#  First escape escapes second one, but the third one is left "naked",  30&nbsp;#+ with same result as first instance, above.  31&nbsp;  32&nbsp;variable=\\\\  33&nbsp;echo "$variable"        # \\  34&nbsp;                        # Second and fourth escapes escaped.  35&nbsp;                        # This is o.k.</PRE></TD></TR></TABLE>      </P></TD></TR></TABLE></DIV><P>Escaping a space can prevent word splitting in a command's argument list.        <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;file_list="/bin/cat /bin/gzip /bin/more /usr/bin/less /usr/bin/emacs-20.7"   2&nbsp;# List of files as argument(s) to a command.   3&nbsp;   4&nbsp;# Add two files to the list, and list all.   5&nbsp;ls -l /usr/X11R6/bin/xsetroot /sbin/dump $file_list   6&nbsp;   7&nbsp;echo "-------------------------------------------------------------------------"   8&nbsp;   9&nbsp;# What happens if we escape a couple of spaces?  10&nbsp;ls -l /usr/X11R6/bin/xsetroot\ /sbin/dump\ $file_list  11&nbsp;# Error: the first three files concatenated into a single argument to 'ls -l'  12&nbsp;#        because the two escaped spaces prevent argument (word) splitting.</PRE></TD></TR></TABLE></P><P><ANAME="ESCNEWLINE"></A></P><P>The <SPANCLASS="TOKEN">escape</SPAN> also provides a means of writing a	multi-line command. Normally, each separate line constitutes	a different command, but an <SPANCLASS="TOKEN">escape</SPAN> at the end	of a line <SPANCLASS="emphasis"><ICLASS="EMPHASIS">escapes the newline character</I></SPAN>,	and the command sequence continues on to the next line.</P><P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;(cd /source/directory &#38;&#38; tar cf - . ) | \   2&nbsp;(cd /dest/directory &#38;&#38; tar xpvf -)   3&nbsp;# Repeating Alan Cox's directory tree copy command,   4&nbsp;# but split into two lines for increased legibility.   5&nbsp;   6&nbsp;# As an alternative:   7&nbsp;tar cf - -C /source/directory . |   8&nbsp;tar xpvf - -C /dest/directory   9&nbsp;# See note below.  10&nbsp;# (Thanks, St閜hane Chazelas.)</PRE></TD></TR></TABLE>        	<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>If a script line ends with a <SPANCLASS="TOKEN">|</SPAN>, a pipe	  character, then a <SPANCLASS="TOKEN">\</SPAN>, an escape, is not strictly	  necessary. It is, however, good programming practice to always	  escape the end of a line of code that continues to the	  following line.</P></TD></TR></TABLE></DIV></P><P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;echo "foo   2&nbsp;bar"    3&nbsp;#foo   4&nbsp;#bar   5&nbsp;   6&nbsp;echo   7&nbsp;   8&nbsp;echo 'foo   9&nbsp;bar'    # No difference yet.  10&nbsp;#foo  11&nbsp;#bar  12&nbsp;  13&nbsp;echo  14&nbsp;  15&nbsp;echo foo\  16&nbsp;bar     # Newline escaped.  17&nbsp;#foobar  18&nbsp;  19&nbsp;echo  20&nbsp;  21&nbsp;echo "foo\  22&nbsp;bar"     # Same here, as \ still interpreted as escape within weak quotes.  23&nbsp;#foobar  24&nbsp;  25&nbsp;echo  26&nbsp;  27&nbsp;echo 'foo\  28&nbsp;bar'     # Escape character \ taken literally because of strong quoting.  29&nbsp;#foo\  30&nbsp;#bar  31&nbsp;  32&nbsp;# Examples suggested by St閜hane Chazelas.</PRE></TD></TR></TABLE></P></DIV><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="quoting.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="exit-status.html"ACCESSKEY="N">Next</A></TD></TR><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top">Quoting</TD><TDWIDTH="34%"ALIGN="center"VALIGN="top"><AHREF="quoting.html"ACCESSKEY="U">Up</A></TD><TDWIDTH="33%"ALIGN="right"VALIGN="top">Exit and Exit Status</TD></TR></TABLE></DIV></BODY></HTML>

⌨️ 快捷键说明

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