10.11. Quoting

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

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

  2. Single quotes will protect double quotes, and double quotes will protect single quotes.

  3. Single quotes protect all metacharacters from interpretation, with the exception of the history character (!).

  4. Double quotes protect all metacharacters from interpretation, with the exception of the history character (!), the variable substitution character ($), and the back quotes (used for command substitution).

10.11.1. The Backslash

The backslash is used to quote a single character and is the only character that can be used to escape a history character sequence (such as !! or !string or !5.) Often the backslash is used to escape the newline character. The special tcsh variable, backslash_quote, can be used for quoting quotes and backslashes, but will not work in csh scripts. (See Example 10.80.)

Example 10.80.
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
						?
						I 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 'I can't help it!'
						Unmatched `.

6  > set backslash_quote
   > echo 'I can't help it!'
						I can't help it!
					

Explanation

  1. 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. "

  2. The shell will not try to interpret the question mark, because it is escaped with the backslash.

  3. The string is continued to the next line by escaping the newline with a backslash. (The ? is the secondary prompt, prompt2).

  4. If the backslash is enclosed in either single or double quotes, it is printed. When not enclosed in quotes, the backslash escapes itself.

  5. When enclosed in single quotes the backslash is ignored as a quoting character; i.e., it will not protect the single apostrophe in can't. The shell sees three single quotes and reports an unmatched quote.

  6. The tcsh variable backslash_quote, if set, causes backslashes to always quote backslashes, single quotes, and double quotes.

10.11.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 (!) which is not protected because the shell evaluates history before it does quotes, but not before backslashes.)

Example 10.81.
1  > echo 'I need $5.00'
						I need $5.00

2  > cp file1 file2
   > echo 'I need $500.00 now!!'
						echo 'I need $500.00 nowcp file1 file2'

3  > echo 'I need $500.00 now!!'
   I need $500.00 now!!

4  > echo 'This is going to be a long line so
						Unmatched '.

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

  1. The string is enclosed in single quotes. All characters, except the history (bang) character (!), are protected from shell interpretation.

  2. After you use the cp command, the echo program is executed. Because the !! was not protected from shell interpretation, by using the backslash character, the last command on the history list is reexecuted and the cp command becomes part of the string.

  3. By quoting the history characters (!!) with backslashes, they are protected from history substitution.

  4. The quotes must be matched on the same line, or the shell reports " Unmatched. "

  5. 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. (The ? is the secondary prompt, prompt2.)

10.11.3. Double Quotes

Double quotes must be matched, will allow variable and command substitution, and hide everything else, except the history (bang (!)). The backslash will not escape the dollar sign when enclosed in double quotes.

Example 10.82.
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!
						echo "Wowecho "Wow!! ""

4  > echo "WOW!!"
						Wow!!
 
5  > echo "I need $5.00"
						I need .00
					

Explanation

  1. The local variable name is assigned the value Bob. The double quotes allow the dollar sign to be used for variable substitution.

  2. The single quote is protected within double quotes.

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

  4. The backslash is used to protect the exclamation point.

  5. The backslash does not escape the dollar sign when used within double quotes.

10.11.4. Combining Double and Single Quotes

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.

Example 10.83.
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

  1. The local variable name is assigned Tom.

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

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

10.11.5. Steps to Successful Quoting

In a more complex command, it is often difficult to match quotes properly unless you follow the steps listed here. (See Appendix C.)

  1. Know the Linux command and its syntax. Before variable substitution, hard code the values into the command line, to see if you get the expected results.

    > awk -F: '/^Zippy Pinhead/{print "Phone is  " $2}' datafile
    Phone is 408-123-4563
    
  2. If the Linux command worked correctly, then plug in the variables. At this point, do not remove or change any quotes. Simply put the variables in place of the words they represent. In this example, replace Zippy Pinhead with $name.

    > set name = "Zippy Pinhead">
    awk -F: '/^$name /{print "Phone is " $2}' datafile
    
  3. Play the quoting game as follows: Starting at the left-hand side with the first single quote, insert a matching single quote just before the dollar sign in $name. Now you have a set of matched quotes.

    awk -F: ' /^'$name /{print "Phone is " $2}' datafile
    

    Now, right after the last letter, e in $name, place another single quote. (Believe me, this works.) This quote matches the quote after the closing curly brace.

    > awk -F: ' /^'$name' /{print "Phone is  " $2} ' datafile
    

    Count the number of single quotes, starting at the left-hand side. You have four, a nice even number. Everything within each set of single quotes is ignored by the shell. The quotes are matched as follows:

    								
    									
    										
    									
    								
    							
  4. Last step: Double quote the variables. Surround each variable very snugly within a set of double quotes. The double quotes protect the white space in the expanded variable; for example, the space in Zippy Pinhead is protected.

    								
    									
    										
    									
    								
    							

10.11.6. Quoting Variables

The :x and :q modifiers are used when it's necessary to quote variables.

Quoting with the :q Modifier

The :q modifier is used to replace double quotes.

Example 10.84.
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

  1. The variable is assigned the string "Daniel Savage."

  2. When :q is appended to the variable, the variable is quoted. This is the same as enclosing the variable in double quotes.

  3. The double quotes surrounding the variable $name allow variable substitution to take place, but protect any white space characters. Without the double quotes, the grep program will search for Daniel in a file called Savage and a file called database.

  4. The variable food is assigned the string "apple pie".

  5. The variable dessert is assigned an array (wordlist) consisting of " apple pie " and " ice cream. "

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

  7. The first element of the array is printed. The variable expands to separated words if not quoted.

  8. The second element of the array is printed.

  9. Since " ice cream " is quoted, it is treated as one word.

  10. 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; i.e., $food:q is the same as "$food".

  11. The array consists of two strings, apple pie and ice cream.

  12. The first element of the array, apple pie, is printed.

  13. 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 10.85.
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
						

Explanation

  1. The variable things is assigned a string. Each string contains a wildcard. The number of elements in the variable is one, one string.

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

  3. The :x extension prevents the shell from expanding the wildcards in the things variable.

  4. The array newthings consists of three elements.

  5. To print the elements of the array, they must be quoted or, again, the shell will try to expand the wildcards.

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

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.135.191.86