9.12. Quoting
The C/TC shell has a whole set of metacharacters that have some special meaning. In fact, almost any character on your keyboard that is not a letter or a number has some special meaning for the shell. Here is a partial list:
* ? [ ] $ ~ ! ^ & { } ( ) > < | ; : %
The backslash and quotes are used to escape the interpretation of metacharacters by the shell. Whereas the backslash is used to escape a single character, the quotes can be used to protect a string of characters. There are some general rules for using quotes:
Quotes are paired and must be matched on a line. The backslash character can be used to escape a newline so that a quote can be matched on the next line. Single quotes will protect double quotes, and double quotes will protect single quotes. Single quotes protect all metacharacters from interpretation, with the exception of the history character (!). Double quotes protect all metacharacters from interpretation, with the exception of the history character (!), the variable substitution character ($), and the backquotes (used for command substitution).
9.12.1 The Backslash
The backslash is used to escape the interpretation of a single character and, in the C shell, is the only character that can be used to escape the history character, the exclamation point (also called the bang). Often the backslash is used to escape the newline character. Backslash interpretation does not take place within quotes.
Example 9.63.
1 % echo Who are you?
echo: No match.
2 % echo Who are you\?
Who are you?
3 % echo This is a very,very long line and this is where \
break the line.
This is a very, very long line and this is where
I break the line.
4 % echo "\\abc"
\\abc
% echo '\\abc'
\\abc
% echo \\abc
\abc
5 % echo Wow\!
Wow!
EXPLANATION
The question mark is used for filename expansion. It matches for a single character. The shell is looking for a file in the current directory that is spelled y–o–u, followed by a single character. Because there is not a file by that name in the directory, the shell complains that it could not find a match with No match. The shell will not try to interpret the question mark, because it is escaped with the backslash. The string is continued to the next line by escaping the newline with a backslash. If the backslash is enclosed in either single or double quotes, it is printed. When not enclosed in quotes, the backslash escapes itself. The exclamation point must be escaped with a backslash. Otherwise the C shell will perform history substitution.
9.12.2 Single Quotes
Single quotes must be matched on the same line and will escape all metacharacters with the exception of the history (bang) character (!). The history character is not protected because the C shell evaluates history before it does quotes, but not before backslashes.
Example 9.64.
1 % echo 'I need $5.00'
I need $5.00
2 % echo 'I need $500.00 now\!\!'
I need $500.00 now!!
3 % echo 'This is going to be a long line so
Unmatched '.
4 % echo 'This is going to be a long line so \
I used the backslash to suppress the newline'
This is going to be a long line so
I used the backslash to suppress the newline
EXPLANATION
The string is enclosed in single quotes. All characters, except the history (bang) character (!), are protected from shell interpretation. The !! must be protected from shell interpretation by using the backslash character. The quotes must be matched on the same line, or the shell reports Unmatched '. If the line is to be continued, the backslash character is used to escape the newline character. The quote is matched at the end of the next line. Even though the shell ignored the newline, the echo command did not.
9.12.3 Double Quotes
Double quotes must be matched, will allow variable and command substitution, and hide everything else except the history (bang) character (!). The backslash will not escape the dollar sign when enclosed in double quotes.
Example 9.65.
1 % set name = Bob
% echo "Hi $name"
Hi Bob
2 % echo "I don't have time."
I don't have time.
3 % echo "WOW!" # Watch the history metacharacter!
": Event not found.
4 % echo "Whoopie\!"
Whoopie!
5 % echo "I need \$5.00"
I need \.00
EXPLANATION
The local variable name is assigned the value Bob. The double quotes allow the dollar sign to be used for variable substitution. The single quote is protected within double quotes. Double or single quotes will not protect the exclamation point from shell interpretation. The built-in history command is looking for the last command that began with a double quote and that event was not found. The backslash is used to protect the exclamation point. The backslash does not escape the dollar sign when used within double quotes.
9.12.4 The Quoting Game
As long as the quoting rules are adhered to, double quotes and single quotes can be used in a variety of combinations in a single command. (See Chapter 15, "Debugging Shell Scripts," on page 967 for a complete discussion on quoting.)
Example 9.66.
1 % set name = Tom
2 % echo "I can't give $name" ' $5.00\!'
I can't give Tom $5.00!
3 % echo She cried, \"Oh help me\!' "', $name.
She cried, "Oh help me!", Tom.
EXPLANATION
The local variable name is assigned Tom. The single quote in the word can't is protected when enclosed within double quotes. The shell would try to perform variable substitution if the dollar sign in $5.00 were within double quotes. Therefore, the string $5.00 is enclosed in single quotes so that the dollar sign will be a literal. The exclamation point is protected with a backslash because neither double nor single quotes can protect it from shell interpretation. The first conversational quotes are protected by the backslash. The exclamation point is also protected with a backslash. The last conversational quotes are enclosed in a set of single quotes. Single quotes will protect double quotes.
Quoting with the :q Modifier
The :q modifier is used to replace double quotes.
Example 9.67.
1 % set name = "Daniel Savage"
2 % grep $name:q database
same as
3 % grep "$name" database
4 % set food = "apple pie"
5 % set dessert = ( $food "ice cream")
6 % echo $#dessert
3
7 % echo $dessert[1]
apple
8 % echo $dessert[2]
pie
9 % echo $dessert[3]
ice cream
10 % set dessert = ($food:q "ice cream")
11 % echo $#dessert
2
12 % echo $dessert[1]
apple pie
13 % echo $dessert[2]
ice cream
EXPLANATION
The variable is assigned the string Daniel Savage. When :q is appended to the variable, the variable is quoted. This is the same as enclosing the variable in double quotes. The double quotes surrounding the variable $name allow variable substitution to take place, but protect any whitespace characters. Without the double quotes, the grep program will search for Daniel in a file called Savage and a file called database. The variable food is assigned the string apple pie. The variable dessert is assigned an array (wordlist) consisting of apple pie and ice cream. The number of elements in the dessert array is three. When the food variable was expanded, the quotes were removed. There are three elements, apple, pie, and ice cream. The first element of the array is printed. The variable expands to separated words if not quoted. The second element of the array is printed. Because "ice cream" is quoted, it is treated as one word. The dessert array is assigned apple pie and ice cream. The :q can be used to quote the variable in the same way double quotes quote the variable; that is, $food:q is the same as "$food". The array consists of two strings, apple pie and ice cream. The first element of the array, apple pie, is printed. The second element of the array, ice cream, is printed.
Quoting with the :x Modifier
If you are creating an array and any of the words in the list contain metacharacters, :x prevents the shell from interpreting the metacharacters when performing variable substitution.
Example 9.68.
1 % set things = "*.c a?? file[1–5]"
% echo $#things
1
2 % set newthings = ( $things )
set: No match.
3 % set newthings = ( $things:x )
4 % echo $#newthings
3
5 % echo "$newthings[1] $newthings[2] $newthings[3] "
*.c a?? file[1-5]
6 % grep $newthings[2]:q filex
The question marks in a?? would be used for filename expansion
it is not quoted
EXPLANATION
The variable things is assigned a string. Each string contains a wildcard. The number of elements in the variable is one, one string. When attempting to create an array out of the string things, the C shell tries to expand the wildcard characters to perform filename substitution within things and produces a No match. The :x extension prevents the shell from expanding the wildcards in the things variable. The array newthings consists of three elements. To print the elements of the array, they must be quoted or, again, the shell will try to expand the wildcards. The :q quotes the variable just as though the variable were surrounded by double quotes. The grep program will print any lines containing the pattern a?? in file filex.
|