266-269.html
来自「linux-unix130.linux.and.unix.ebooks130 l」· HTML 代码 · 共 104 行
HTML
104 行
<HTML>
<HEAD>
<TITLE>Linux Unleashed, Third Edition:Shell Programming</TITLE>
<SCRIPT>
<!--
function displayWindow(url, width, height) {
var Win = window.open(url,"displayWindow",'width=' + width +
',height=' + height + ',resizable=1,scrollbars=yes');
}
//-->
</SCRIPT>
</HEAD>
-->
<!--ISBN=0672313723//-->
<!--TITLE=Linux Unleashed, Third Edition//-->
<!--AUTHOR=Tim Parker//-->
<!--PUBLISHER=Macmillan Computer Publishing//-->
<!--IMPRINT=Sams//-->
<!--CHAPTER=14//-->
<!--PAGES=266-269//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//-->
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="264-266.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="269-270.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<H3><A NAME="Heading7"></A><FONT COLOR="#000077">The Importance of Quotation Marks</FONT></H3>
<P>The use of the different types of quotation marks is very important in shell programming. All three kinds of quotation marks and the backslash character are used by the shell to perform different functions. The double quotation marks (<TT>“”</TT>), the single quotation marks (<TT>’’</TT>), and the backslash (<TT>\</TT>) are all used to hide special characters from the shell. Each of these methods hides varying degrees of special characters from the shell.</P>
<P>The double quotation marks are the least powerful of the three methods. When you surround characters with double quotes, all the whitespace characters are hidden from the shell, but all other special characters are still interpreted by the shell. This type of quoting is most useful when you are assigning strings that contain more than one word to a variable. For example, if you want to assign the string <TT>hello there</TT> to the variable <TT>greeting</TT>, type the following command:</P>
<!-- CODE SNIP //-->
<PRE>
greeting=”hello there” (for bash and pdksh)
set greeting = “hello there” (for tcsh)
</PRE>
<!-- END CODE SNIP //-->
<P>This command stores the <TT>hello there</TT> string into the <TT>greeting</TT> variable as one word. If you type this command without using the quotes, you do not get the results that you want. <TT>bash</TT> and <TT>pdksh</TT> will not understand the command and will return an error message. <TT>tcsh</TT> simply assigns the value <TT>hello</TT> to the <TT>greeting</TT> variable and ignores the rest of the command line.</P>
<P>Single quotes are the most powerful form of quoting. They hide all special characters from the shell. This is useful if the command that you enter is intended for a program other than the shell.</P>
<P>Because the single quotes are the most powerful, you can write the <TT>hello there</TT> variable assignment using single quotes. You may not always want to do this. If the string being assigned to the <TT>greeting</TT> variable contains another variable, you have to use the double quotes. For example, if you want to include the name of the user in your greeting, then type the following command:</P>
<!-- CODE SNIP //-->
<PRE>
greeting=”hello there $LOGNAME” (for bash and pdksh)
set greeting=”hello there $LOGNAME” (for tcsh)
</PRE>
<!-- END CODE SNIP //-->
<BLOCKQUOTE>
<P><FONT SIZE="-1"><HR><B>Tip: </B><BR>Remember that the <TT>LOGNAME</TT> variable is a shell variable that contains the Linux username of the person who is logged in to the system.<HR></FONT>
</BLOCKQUOTE>
<P>This stores the value <TT>hello there root</TT> into the <TT>greeting</TT> variable if you are logged in to Linux as <TT>root</TT>. If you try to write this command using single quotes, it will not work because the single quotes hide the dollar sign from the shell and the shell doesn’t know that it is supposed to perform a variable substitution. The <TT>greeting</TT> variable will be assigned the value <TT>hello there $LOGNAME</TT> if you write the command using single quotes.</P>
<P>Using the backslash is the third way of hiding special characters from the shell. Like the single quotation mark method, the backslash hides all special characters from the shell, but it can hide only one character at a time, as opposed to groups of characters. You can rewrite the greeting example using the backslash instead of double quotation marks using the following command:</P>
<!-- CODE SNIP //-->
<PRE>
greeting=hello\ there (for bash and pdksh)
set greeting=hello\ there (for tcsh)
</PRE>
<!-- END CODE SNIP //-->
<P>In this command, the backslash hides the space character from the shell, and the string <TT>hello there</TT> is assigned to the <TT>greeting</TT> variable.</P>
<P>Backslash quoting is used most often when you want to hide only a single character from the shell. This is usually done when you want to include a special character in a string. For example, if you want to store the price of a box of computer disks into a variable named <TT>disk_price</TT>, use the following command:</P>
<!-- CODE SNIP //-->
<PRE>
disk_price=\$5.00 (for bash and pdksh)
set disk_price = \$5.00 (for tcsh)
</PRE>
<!-- END CODE SNIP //-->
<P>The backslash in this example will hide the dollar sign from the shell. If the backslash were not there, the shell would try to find a variable named <TT>5</TT> and perform a variable substitution on that variable. Assuming that no variable named <TT>5</TT> is defined, the shell assigns a value of <TT>.00</TT> to the <TT>disk_price</TT> variable. This is because the shell substitutes a value of null for the <TT>$5</TT> variable.</P>
<BLOCKQUOTE>
<P><FONT SIZE="-1"><HR><B>Note: </B><BR>The <TT>disk_price</TT> example could also have used single quotes to hide the dollar sign from the shell.<HR></FONT>
</BLOCKQUOTE>
<P>The back quotation marks (<TT>‘</TT>) perform a different function. They are used when you want to use the results of a command in another command. For example, if you want to set the value of the variable <TT>contents</TT> equal to the list of files in the current directory, type the following command:</P>
<!-- CODE SNIP //-->
<PRE>
contents=‘ls‘ (for bash and pdksh)
set contents = ‘ls‘ (for tcsh)
</PRE>
<!-- END CODE SNIP //-->
<P>This command executes the <TT>ls</TT> command and stores the results of the command into the <TT>contents</TT> variable. As you’ll see in the upcoming section “Iteration Statements,” this feature can be very useful when you want to write a shell program that performs some action on the results of another command.</P><P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="264-266.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="269-270.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
</td>
</tr>
</table>
<!-- begin footer information -->
</body></html>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?