7.7. Quoting

 <  Day Day Up  >  

Quoting 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.

Table 7.8. Special Metacharacters Requiring Quotes

Metacharacter

Meaning

;

Command separator

&

Background processing

( )

Command grouping; creates a subshell

{ }

Command grouping; does not create a subshell

Pipe

<

Input redirection

>

Output redirection

newline

Command termination

space/tab

Word delimiter

$

Variable substitution character

* [ ] ?

Shell metacharacters for filename expansion


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 Backslash

The 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"   .00  

EXPLANATION

  1. The backslash prevents the shell from performing filename substitution on the question mark.

  2. The backslash escapes the newline, allowing the next line to become part of this line.

  3. Because the backslash itself is a special character, it prevents the backslash following it from interpretation.

  4. The backslash is not interpreted when enclosed in single quotes.

  5. All characters in single quotes are treated literally. The backslash does not serve any purpose here.

  6. When enclosed in double quotes, the backslash prevents the dollar sign from being interpreted for variable substitution.

7.7.2 Single Quotes

Single 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 .00?'   Don't you need .00?  3   $  echo 'Mother yelled, "Time to eat!"'   Mother yelled, "Time to eat!"  

EXPLANATION

  1. The single quote is not matched on the line. The Bourne shell produces a secondary prompt. It is waiting for the quote to be matched.

  2. The single quotes protect all metacharacters from interpretation. The apostrophe in Don't is escaped with a backslash. Otherwise, it would match the first quote, and the single quote at the end of the string would not have a mate. In this example, the $ and the ? are protected from the shell and will be treated as literals.

  3. The single quotes protect the double quotes in this string.

7.7.3 Double Quotes

Double 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

  1. The variable name is assigned the string Jody .

  2. The double quotes surrounding the string will protect all special metacharacters from interpretation, with the exception of $ in $name . Variable substitution is performed within double quotes.

  3. Variable substitution and command substitution are both performed when enclosed within double quotes. The variable name is expanded, and the command in backquotes, date , is executed.

 <  Day Day Up  >  


UNIX Shells by Example
UNIX Shells by Example (4th Edition)
ISBN: 013147572X
EAN: 2147483647
Year: 2004
Pages: 454
Authors: Ellie Quigley

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net