< Day Day Up > |
7.7. QuotingQuoting is used to protect special metacharacters from interpretation. There are three methods of quoting: the backslash, single quotes, and double quotes. The characters listed in Table 7.8 are special to the shell and must be quoted.
Single and double quotes must be matched. Single quotes protect special metacharacters, such as $, *, ?, |, >, and <, from interpretation. Double quotes also protect special metacharacters from being interpreted, but allow variable and command substitution characters (the dollar sign and backquotes) to be processed. Single quotes will protect double quotes and double quotes will protect single quotes. The Bourne shell does not let you know if you have mismatched quotes. If running interactively, a secondary prompt appears when quotes are not matched. If in a shell script, the file is scanned and if the quote is not matched, the shell will attempt to match it with the next available quote; if the shell cannot match it with the next available quote, the program aborts and the message 'end of file' unexpected appears on the terminal. Quoting can be a real hassle for even the best of shell programmers! See "What You Should Know About Quotes" on page 985 for shell quoting rules 7.7.1 The BackslashThe backslash is used to quote (or escape) a single character from interpretation. The backslash is not interpreted if placed in single quotes. The backslash will protect the dollar sign ($), backquotes (` `), and the backslash from interpretation if enclosed in double quotes. Example 7.31.1 $ echo Where are you going\? Where are you going? 2 $ echo Start on this line and \ > go to the next line. Start on this line and go to the next line. 3 $ echo \\ \ 4 $ echo '\\' \\ 5 $ echo '\$5.00' \$5.00 6 $ echo "\$5.00" $5.00 EXPLANATION
7.7.2 Single QuotesSingle quotes must be matched. They protect all metacharacters from interpretation. To print a single quote, it must be enclosed in double quotes or escaped with a backslash. Example 7.32.1 $ echo 'hi there > how are you? > When will this end? > When the quote is matched > oh' hi there how are you? When will this end? When the quote is matched oh 2 $ echo 'Don\'t you need $5.00?' Don't you need $5.00? 3 $ echo 'Mother yelled, "Time to eat!"' Mother yelled, "Time to eat!" EXPLANATION
7.7.3 Double QuotesDouble quotes must be matched, will allow variable and command substitution, and protect any other special metacharacters from being interpreted by the shell. Example 7.33.1 $ name=Jody 2 $ echo "Hi $name, I'm glad to meet you!" Hi Jody, I'm glad to meet you! 3 $ echo "Hey $name, the time is `date`" Hey Jody, the time is Wed Oct 13 14:04:11 PST 2004 EXPLANATION
|
< Day Day Up > |