Previous Section  < Day Day Up >  Next Section

13.11. Quoting

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

Table 13.22. 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.

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

  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. The backslash is not interpreted when inside single quotes; therefore, the shell sees three single quotes (the one at the end of the string is not matched). A secondary prompt appears, waiting for a closing single quote. When the shell finally gets the closing quote, it strips out all of the quotes and passes the string on to the echo command. Because the first two quotes were matched, the rest of the string, t you need $5.00?, was not enclosed within any quotes. The shell tried to evaluate $5; it was empty and .00 printed.

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

  1. The single quote is not matched on the line. The bash 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 (the backslash protects a single character, rather than a string). Otherwise, it would match the single quote before the $. Then the single quote at the end of the string would not have a mate. The $ and the ? are enclosed in a pair of single quotes, protecting them from shell interpretation, thus treating them as literals.

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

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

  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 parentheses, date, is executed. (See "Command Substitution," next.)

    Previous Section  < Day Day Up >  Next Section