Previous | Table of Contents | Next |
Single quotes can sometimes take away too much of the shells special conveniences. The following echo statement contains many special characters that must be quoted in order to use them literally:
echo '$USER owes <-$1250.**>; [ as of ('date +%m/%d') ]'
The output using single quotes is easy to predictwhat you see is what you get:
$USER owes <-$1250.**>; [ as of ('date +%m/%d') ]
However, this is not exactly what you want in this case. Single quotes prevent variable substitution (covered in Chapter 8, Substitution), so $USER is not replaced by the specific user name stored in that variable. Single quotes also prevent command substitution (covered in Chapter 8), so the attempt to insert the current month and day using the date command within backquotes fails.
Double quotes are the answer to this situation.
Double quotes take away the special meaning of all characters except the following:
Watch what happens if you use double quotes like this:
echo "$USER owes <-$1250.**>; [ as of ('date +%m/%d') ]"
Notice in the following output that the double quotes enable variable substitution to replace $USER and command substitution to replace 'date +%m/%d':
Fred owes <-250.**>; [ as of (12/21) ]
As you can see in this example, double quotes permit you to display many special characters literally while still enabling $ and backquote substitutions. However, notice the amount of money owed is incorrect because $1 is substituted. To correct this, always use a leading backslash to escape any $ within double quotes where substitution is not intended:
echo "$USER owes <-\$1250.**>; [ as of ('date +%m/%d') ]""
The escaped dollar sign is no longer a special character, so the dollar amount appears correctly in the output now:
Fred owes <-$1250.**>; [ as of (12/21) ]
Now that you have seen all three forms of quoting, here is a summary of their usage:
Quoting character | Effect | |
---|---|---|
Single quote | All special characters between these quotes lose their special meaning. | |
Double quote | Most special characters between these quotes lose their special meaning with these exceptions: | |
$ | ||
| ||
\$ | ||
\ | ||
\ | ||
\\ | ||
Backslash | Any character immediately following the backslash loses its special meaning. | |
This table also shows that double quotes or backslashes can be embedded in a double quoted string if they are escaped:
echo "The DOS directory is \"\\windows\\temp\""
The output looks like this:
The DOS directory is "\windows\temp"
Now that you know the basics, you can learn some additional rules to help you use quoting. You can also look at various UNIX commands and apply quoting to other situations.
In English, you are used to quoting whole words or sentences. In shell programming, the special characters must be quoted, but it does not matter whether the regular characters are quoted in the same word, as follows:
echo "Hello; world"
You can move the quotes off word boundaries as long as any special characters remain quoted. This command produces the same output as the preceding one:
echo Hel"lo; w"orld
Of course, it is easier to read the line if the quotes are on word boundaries. I present this point here to help you understand quoting and because you need this knowledge for more complex quoting situations.
You can freely switch from one type of quoting to another within the same command. This example contains single quotes, a backslash, and then double quotes:
echo The '$USER' variable contains this value \> "|$USER|"
Here is the output of this command if fred is the current content of $USER:
The $USER variable contains this value > |fred|
To the shell, one or more spaces or tabs form a single command line argument separator. For example,
echo Name Address
displays as
Name Address
Even though you put multiple spaces between Name and Address, the shell regards them as special characters forming one separator. The echo command simply displays the arguments it has received separated by a single space.
You can quote the spaces to achieve the desired result:
echo "Name Address"
Now the multiple spaces are preserved in the output:
Name Address
Spaces must also be quoted to embed them in a single command line argument:
mail -s Meeting tomorrow fred jane < meeting.notice
The mail command enables you to send mail to a list of users. The -s option enables the following argument to be used as the subject of the mail. The word tomorrow is supposed to be part of the subject, but it is taken as one of the users to receive the message and causes an error. You can solve this by quoting the embedded space within the subject using any of the three types of quoting:
mail -s Meeting\ tomorrow fred jane < meeting.notice mail -s 'Meeting tomorrow' fred jane < meeting.notice mail -s "Meeting tomorrow" fred jane < meeting.notice
Previous | Table of Contents | Next |