📄 special-chars.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><HTML><HEAD><TITLE>Special Characters</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="Basics"HREF="part2.html"><LINKREL="NEXT"TITLE="Introduction to Variables and Parameters"HREF="variables.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="part2.html"ACCESSKEY="P">Prev</A></TD><TDWIDTH="80%"ALIGN="center"VALIGN="bottom"></TD><TDWIDTH="10%"ALIGN="right"VALIGN="bottom"><AHREF="variables.html"ACCESSKEY="N">Next</A></TD></TR></TABLE><HRALIGN="LEFT"WIDTH="100%"></DIV><DIVCLASS="CHAPTER"><H1><ANAME="SPECIAL-CHARS"></A>Chapter 3. Special Characters</H1><P>What makes a character <ICLASS="FIRSTTERM">special</I>? If it has a meaning beyond its <ICLASS="FIRSTTERM">literal meaning</I>, a <AHREF="regexp.html#METAMEANINGREF">meta-meaning</A>, then we refer to it as a <ICLASS="FIRSTTERM">special character</I>.</P><DIVCLASS="VARIABLELIST"><P><B><ANAME="SCHARLIST1"></A>Special Characters Found In Scripts and Elsewhere</B></P><DL><DT><ANAME="HASHMARKREF"></A><SPANCLASS="TOKEN">#</SPAN></DT><DD><DIVCLASS="FORMALPARA"><P><B>Comments. </B>Lines beginning with a <SPANCLASS="TOKEN">#</SPAN> (<AHREF="sha-bang.html#MAGNUMREF">with the exception of <SPANCLASS="TOKEN">#!</SPAN></A>) are comments and will <SPANCLASS="emphasis"><ICLASS="EMPHASIS">not</I></SPAN> be executed.</P></DIV><P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING"> 1 # This line is a comment.</PRE></TD></TR></TABLE></P><P>Comments may also occur following the end of a command.</P><P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING"> 1 echo "A comment will follow." # Comment here. 2 # ^ Note whitespace before #</PRE></TD></TR></TABLE></P><P><ANAME="WSBCOMM"></A></P><P>Comments may also follow <AHREF="special-chars.html#WHITESPACEREF">whitespace</A> at the beginning of a line.</P><P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING"> 1 # A tab precedes this comment.</PRE></TD></TR></TABLE></P><DIVCLASS="CAUTION"><TABLECLASS="CAUTION"WIDTH="90%"BORDER="0"><TR><TDWIDTH="25"ALIGN="CENTER"VALIGN="TOP"><IMGSRC="common/caution.png"HSPACE="5"ALT="Caution"></TD><TDALIGN="LEFT"VALIGN="TOP"><P>A command may not follow a comment on the same line. There is no method of terminating the comment, in order for <SPANCLASS="QUOTE">"live code"</SPAN> to begin on the same line. Use a new line for the next command.</P></TD></TR></TABLE></DIV><DIVCLASS="NOTE"><TABLECLASS="NOTE"WIDTH="90%"BORDER="0"><TR><TDWIDTH="25"ALIGN="CENTER"VALIGN="TOP"><IMGSRC="common/note.png"HSPACE="5"ALT="Note"></TD><TDALIGN="LEFT"VALIGN="TOP"><P>Of course, an <AHREF="escapingsection.html#ESCP">escaped</A> <SPANCLASS="TOKEN">#</SPAN> in an <BCLASS="COMMAND">echo</B> statement does <SPANCLASS="emphasis"><ICLASS="EMPHASIS">not</I></SPAN> begin a comment. Likewise, a <SPANCLASS="TOKEN">#</SPAN> appears in <AHREF="parameter-substitution.html#PSUB2">certain parameter substitution constructs</A> and in <AHREF="numerical-constants.html#NUMCONSTANTS"> numerical constant expressions</A>. <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING"> 1 echo "The # here does not begin a comment." 2 echo 'The # here does not begin a comment.' 3 echo The \# here does not begin a comment. 4 echo The # here begins a comment. 5 6 echo ${PATH#*:} # Parameter substitution, not a comment. 7 echo $(( 2#101011 )) # Base conversion, not a comment. 8 9 # Thanks, S.C.</PRE></TD></TR></TABLE> The standard <AHREF="quoting.html#QUOTINGREF">quoting and escape</A> characters (" ' \) escape the #. </P></TD></TR></TABLE></DIV><P>Certain <AHREF="parameter-substitution.html#PSOREX1">pattern matching operations</A> also use the <SPANCLASS="TOKEN">#</SPAN>.</P></DD><DT><ANAME="SEMICOLONREF"></A><SPANCLASS="TOKEN">;</SPAN></DT><DD><DIVCLASS="FORMALPARA"><P><B>Command separator [semicolon]. </B>Permits putting two or more commands on the same line.</P></DIV><P> <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING"> 1 echo hello; echo there 2 3 4 if [ -x "$filename" ]; then # Note that "if" and "then" need separation. 5 # Why? 6 echo "File $filename exists."; cp $filename $filename.bak 7 else 8 echo "File $filename not found."; touch $filename 9 fi; echo "File test complete."</PRE></TD></TR></TABLE></P><P>Note that the <SPANCLASS="QUOTE">"<SPANCLASS="TOKEN">;</SPAN>"</SPAN> sometimes needs to be <AHREF="escapingsection.html#ESCP">escaped</A>.</P></DD><DT><SPANCLASS="TOKEN">;;</SPAN></DT><DD><DIVCLASS="FORMALPARA"><P><B>Terminator in a <AHREF="testbranch.html#CASEESAC1">case</A> option [double semicolon]. </B><ANAME="DOUBLESEMICOLON"></A></P></DIV><P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING"> 1 case "$variable" in 2 abc) echo "\$variable = abc" ;; 3 xyz) echo "\$variable = xyz" ;; 4 esac</PRE></TD></TR></TABLE></P></DD><DT><SPANCLASS="TOKEN">.</SPAN></DT><DD><P><ANAME="DOTREF"></A></P><DIVCLASS="FORMALPARA"><P><B><SPANCLASS="QUOTE">"dot"</SPAN> command [period]. </B>Equivalent to <AHREF="internal.html#SOURCEREF">source</A> (see <AHREF="internal.html#EX38">Example 14-22</A>). This is a bash <AHREF="internal.html#BUILTINREF">builtin</A>.</P></DIV></DD><DT><SPANCLASS="TOKEN">.</SPAN></DT><DD><DIVCLASS="FORMALPARA"><P><B><SPANCLASS="QUOTE">"dot"</SPAN>, as a component of a filename. </B>When working with filenames, a dot is the prefix of a <SPANCLASS="QUOTE">"hidden"</SPAN> file, a file that an <AHREF="external.html#LSREF">ls</A> will not normally show. <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="SCREEN"> <TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>touch .hidden-file</B></TT> <TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>ls -l</B></TT> <TTCLASS="COMPUTEROUTPUT">total 10 -rw-r--r-- 1 bozo 4034 Jul 18 22:04 data1.addressbook -rw-r--r-- 1 bozo 4602 May 25 13:58 data1.addressbook.bak -rw-r--r-- 1 bozo 877 Dec 17 2000 employment.addressbook</TT> <TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>ls -al</B></TT> <TTCLASS="COMPUTEROUTPUT">total 14 drwxrwxr-x 2 bozo bozo 1024 Aug 29 20:54 ./ drwx------ 52 bozo bozo 3072 Aug 29 20:51 ../ -rw-r--r-- 1 bozo bozo 4034 Jul 18 22:04 data1.addressbook -rw-r--r-- 1 bozo bozo 4602 May 25 13:58 data1.addressbook.bak -rw-r--r-- 1 bozo bozo 877 Dec 17 2000 employment.addressbook -rw-rw-r-- 1 bozo bozo 0 Aug 29 20:54 .hidden-file</TT> </PRE></TD></TR></TABLE> </P></DIV><P><ANAME="DOTDIRECTORY"></A></P><P>When considering directory names, <ICLASS="FIRSTTERM">a single dot</I> represents the current working directory, and <ICLASS="FIRSTTERM">two dots</I> denote the parent directory.</P><P> <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="SCREEN"> <TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>pwd</B></TT> <TTCLASS="COMPUTEROUTPUT">/home/bozo/projects</TT> <TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>cd .</B></TT> <TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>pwd</B></TT> <TTCLASS="COMPUTEROUTPUT">/home/bozo/projects</TT> <TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>cd ..</B></TT> <TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>pwd</B></TT> <TTCLASS="COMPUTEROUTPUT">/home/bozo/</TT> </PRE></TD></TR></TABLE> </P><P>The <ICLASS="FIRSTTERM">dot</I> often appears as the destination (directory) of a file movement command.</P><P> <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="SCREEN"> <TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>cp /home/bozo/current_work/junk/* .</B></TT> </PRE></TD></TR></TABLE> </P></DD><DT><SPANCLASS="TOKEN">.</SPAN></DT><DD><DIVCLASS="FORMALPARA"><P><B><SPANCLASS="QUOTE">"dot"</SPAN> character match. </B>When <AHREF="regexp.html#REGEXDOT">matching characters</A>, as part of a <AHREF="regexp.html#REGEXREF">regular expression</A>, a <SPANCLASS="QUOTE">"dot"</SPAN> matches a single character.</P></DIV></DD><DT><SPANCLASS="TOKEN">"</SPAN></DT><DD><DIVCLASS="FORMALPARA"><P><B><AHREF="variables.html#DBLQUO">partial quoting</A> [double quote]. </B><SPANCLASS="emphasis"><ICLASS="EMPHASIS">"STRING"</I></SPAN> preserves (from interpretation) most of the special characters within <SPANCLASS="emphasis"><ICLASS="EMPHASIS">STRING</I></SPAN>. See also <AHREF="quoting.html">Chapter 5</A>.</P></DIV></DD><DT><SPANCLASS="TOKEN">'</SPAN></DT><DD><DIVCLASS="FORMALPARA"><P><B><AHREF="variables.html#SNGLQUO">full quoting</A> [single quote]. </B><SPANCLASS="emphasis"><ICLASS="EMPHASIS">'STRING'</I></SPAN> preserves all special characters within <SPANCLASS="emphasis"><ICLASS="EMPHASIS">STRING</I></SPAN>. This is a stronger form of quoting than using <SPANCLASS="TOKEN">"</SPAN>. See also <AHREF="quoting.html">Chapter 5</A>.</P></DIV></DD><DT><SPANCLASS="TOKEN">,</SPAN></DT><DD><DIVCLASS="FORMALPARA"><P><B><AHREF="operations.html#COMMAOP">comma operator</A>. </B>The <ICLASS="FIRSTTERM">comma operator</I> <ANAME="AEN566"HREF="#FTN.AEN566">[1]</A> links together a series of arithmetic operations. All are evaluated, but only the last one is returned. <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING"> 1 let "t2 = ((a = 9, 15 / 3))" # Set "a = 2 9" and "t2 = 15 / 3"</PRE></TD></TR></TABLE> </P></DIV></DD><DT><SPANCLASS="TOKEN">\</SPAN></DT><DD><DIVCLASS="FORMALPARA"><P><B><AHREF="escapingsection.html#ESCP">escape</A> [backslash]. </B>A quoting mechanism for single characters.</P></DIV><P><TTCLASS="USERINPUT"><B>\X</B></TT> <SPANCLASS="QUOTE">"escapes"</SPAN> the character <SPANCLASS="emphasis"><ICLASS="EMPHASIS">X</I></SPAN>. This has the effect of <SPANCLASS="QUOTE">"quoting"</SPAN> <SPANCLASS="emphasis"><ICLASS="EMPHASIS">X</I></SPAN>, equivalent to <SPANCLASS="emphasis"><ICLASS="EMPHASIS">'X'</I></SPAN>. The <SPANCLASS="TOKEN">\</SPAN> may be used to quote <SPANCLASS="TOKEN">"</SPAN> and <SPANCLASS="TOKEN">'</SPAN>, so they are expressed literally.</P><P>See <AHREF="quoting.html">Chapter 5</A> for an in-depth explanation of escaped characters.</P></DD><DT><SPANCLASS="TOKEN">/</SPAN></DT><DD><DIVCLASS="FORMALPARA"><P><B>Filename path separator [forward slash]. </B>Separates the components of a filename (as in <TTCLASS="FILENAME">/home/bozo/projects/Makefile</TT>).</P></DIV><P>This is also the division <AHREF="operations.html#AROPS1">arithmetic operator</A>.</P
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -