< Day Day Up > |
13.11. QuotingQuoting is used to protect special metacharacters from interpretation and prevent parameter expansion. There are three methods of quoting: the backslash, single quotes, and double quotes. The characters listed in Table 13.22 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. Unlike the Bourne shell, bash tries to 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 bash:unexpected EOF while looking for `"` appears on the terminal. Quoting can be a real hassle for even the best of shell programmers! 13.11.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 13.72.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 7 $ echo 'Don\'t you need $5.00?' > >' Don\t you need .00? EXPLANATION
13.11.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 13.73.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
13.11.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 13.74.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 Jul 14 14:04:11 PST 2004 EXPLANATION
|
< Day Day Up > |