Appendix C. Steps for Using Quoting Correctly

  •  C.1 Backslash
  •  C.2 Single Quotes
  •  C.3 Double Quotes
  •  C.4 Combining Quotes
  •  C.5 Setting the Shell Variable

C.1 Backslash

  1. Precedes a character and escapes that character.

  2. Same as putting single quotes around one character.

C.2 Single Quotes

  1. Must be matched.

  2. Protect all metacharacters from interpretation except the following:

    1. Itself.

    2. Exclamation point (csh only).

    3. Backslash.


C Shell Bourne Shell Korn Shell
echo '$><%^&*' echo '$*&!><?' echo '$*&!><?'
echo 'I need $5.00\!' echo 'I need \$5.00!' echo 'I need \$5.00!'
ho 'She cried, "Help"' echo 'She cried, "Help"' echo 'She cried, "Help"'
echo '\\\\' echo '\\\\' print '\\\\'
\\\\ \\ \\

C.3 Double Quotes

  1. Must be matched.

  2. Protect all metacharacters from interpretation except the following:

    1. Itself.

    2. Exclamation point (csh only).

    3. $ used for variable substitution.

    4. ' ' Backquotes for command substitution.


C Shell Bourne Shell Korn Shell
echo "Hello $LOGNAME\!" echo "Hello $LOGNAME!" print "Hello $LOGNAME!"
echo "I don't care" echo "I don't care" print "I don't care"
echo "The date is 'date'" echo "The date is 'date'" print "The date is $(date)"
echo "\\\\" echo "\\\\" print "\\\\"
\\\\ \ \

C.4 Combining Quotes

The end result is to be able to embed the shell variable in the awk command line and have the shell expand the variable without interfering with nawk's field designators, $1 and $2.

C.5 Setting the Shell Variable

name="Jacob Savage"    (Bourne and Korn Shell)
set name = "Jacob Savage" ( C Shell )

(The line from the datafile)
Jacob Savage:408-298-7732:934 La Barbara Dr. , San Jose, CA:02/27/78:500000

(The nawk command line)
nawk -F: '$1 ~ /^' "$name" '/{print $2}' datafile



Test your knowledge of the UNIX command at the command line before plugging in any shell variables.

nawk -F: '$1 ~ /^Jacob Savage/{print $2}' filename


Step 2:

Plug in the shell variable without changing anything else. Leave all quotes as they were.

nawk -F: '$1 ~ /^$name/{print $2}' datafile

Starting at the left-hand side of the awk command leave the first quote as is; right before the shell dollar sign in $name, place another single quote. Now the first quote is matched and all text within these two quotes is protected from shell interference. The variable is exposed. Now put another single quote right after the e in $name. This starts another matched set of single quotes ending after awk's closing curly brace. Everything within this set of quotes is also protected from shell interpretation.


Step 3:

Enclose the shell variable in a set of double quotes. This allows the variable to be expanded but the value of the variable will be treated as single string if it contains whitespace. The whitespace must be protected so that the command line is parsed properly.


Count the number of quotes. There should be an even number of single quotes and an even number of double quotes.


oldname="Ellie Main"
newname="Eleanor Quigley"

  1. Make sure the command works.

    nawk -F: '/^Ellie Main/{$1="Eleanor Quigley"; print $0}' datafile

  2. Plug in the variables.

    nawk -F: '/^$oldname/{$1="$newname"; print $0}' datafile

  3. Play the quoting game. Starting at the first single quote at the left, move across the line until you come to the variable $oldname and place another single quote just before the dollar sign. Put another single quote right after the last letter in the variable name.

    Now move to the right and place another single quote right before the dollar sign in $newname. Put another single quote after the last character in $newname.


  4. Count the number of single quotes. If the number of single quotes is an even number, each quote has a matching quote. If not, you have forgotten a step.

  5. Enclose each of the shell variables in double quotes. The double quotes are placed snugly around the shell variable.




UNIX Shells by Example
UNIX Shells by Example, 3rd Edition
ISBN: 013066538X
EAN: 2147483647
Year: 2001
Pages: 18
Authors: Ellie Quigley

Similar book on Amazon © 2008-2017.
If you may any questions please contact us: