11.10. Quoting

 <  Day Day Up  >  

Quotes are used to protect special metacharacters from interpretation. They can cause major debugging hassles in all shell scripts. Single quotes must be matched. They protect special metacharacters from being interpreted by the shell. Double quotes also must be matched. They protect most characters from interpretation by the shell, but allow variable and command substitution characters to be processed . Single quotes will protect double quotes, and double quotes will protect single quotes. The Korn shell, unlike the Bourne shell, will inform you if you have mismatched quotes by sending an error message to standard error with the line where it detects that the quotes were mismatched.

11.10.1 The Backslash

The backslash is used to protect (or escape) a single character from interpretation.

Example 11.54.
 1   $  print Where are you going\?   Where are you going?  2   $  print Start on this line and \  >  go to the next line.   Start on this line and go to the next line.  

EXPLANATION

  1. The special metacharacter ? is escaped with the backslash. It will not be interpreted for filename expansion by the shell.

  2. The newline is escaped. The next line will become part of the first line. The > is the Korn shell's secondary prompt.

11.10.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 11.55.
 1   $  print '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   $  print 'Do you need .00?'   Do you need .00?  3   $  print 'Mother yelled, "Time to eat!"'   Mother yelled, "Time to eat!"  

EXPLANATION

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

  2. The single quotes protect all metacharacters from interpretation. 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. The double quotes here are conversational quotes.

11.10.3 Double Quotes

Double quotes must be matched. They allow variable and command substitution, and protect any other special metacharacters from being interpreted by the shell.

Example 11.56.
 1   $ name=Jody 2   $  print "Hi $name, I'm glad to meet you!"   Hi Jody, I'm glad to meet you!  3   $  print "Hey $name, the time is `date`"   Hey Jody, the time is Fri Dec 18 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