GNU Info

Info Node: (gawk.info)Quoting

(gawk.info)Quoting


Prev: Comments Up: Running gawk
Enter node , (file) or (file)node

Shell Quoting Issues
--------------------

   For short to medium length `awk' programs, it is most convenient to
enter the program on the `awk' command line.  This is best done by
enclosing the entire program in single quotes.  This is true whether
you are entering the program interactively at the shell prompt, or
writing it as part of a larger shell script:

     awk 'PROGRAM TEXT' INPUT-FILE1 INPUT-FILE2 ...

   Once you are working with the shell, it is helpful to have a basic
knowledge of shell quoting rules.  The following rules apply only to
POSIX-compliant, Bourne-style shells (such as `bash', the GNU
Bourne-Again Shell).  If you use `csh', you're on your own.

   * Quoted items can be concatenated with nonquoted items as well as
     with other quoted items.  The shell turns everything into one
     argument for the command.

   * Preceding any single character with a backslash (`\') quotes that
     character.  The shell removes the backslash and passes the quoted
     character on to the command.

   * Single quotes protect everything between the opening and closing
     quotes.  The shell does no interpretation of the quoted text,
     passing it on verbatim to the command.  It is _impossible_ to
     embed a single quote inside single-quoted text.  Refer back to
     Note: Comments in `awk' Programs, for an example showing
     what happens if you try.

   * Double quotes protect most things between the opening and closing
     quotes.  The shell does at least variable and command substitution
     on the quoted text.  Different shells may do additional kinds of
     processing on double-quoted text.

     Since certain characters within double-quoted text are processed
     by the shell, they must be "escaped" within the text.  Of note are
     the characters `$', ``', `\' and `"', all of which must be
     preceded by a backslash within double-quoted text if they are to
     be passed on literally to the program.  (The leading backslash is
     stripped first.)  Thus, the example seen in Note: Running `awk'
     Without Input Files, is applicable:

          $ awk "BEGIN { print \"Don't Panic!\" }"
          -| Don't Panic!

     Note that the single quote is not special within double quotes.

   * Null strings are removed when they occur as part of a non-null
     command-line argument, while explicit non-null objects are kept.
     For example, to specify that the field separator `FS' should be
     set to the null string, use:

          awk -F "" 'PROGRAM' FILES # correct

     Don't use this:

          awk -F"" 'PROGRAM' FILES  # wrong!

     In the second case, `awk' will attempt to use the text of the
     program as the value of `FS', and the first file name as the text
     of the program!  This results in syntax errors at best, and
     confusing behavior at worst.

   Mixing single and double quotes is difficult.  You have to resort to
shell quoting tricks, like this:

     $ awk 'BEGIN { print "Here is a single quote <'"'"'>" }'
     -| Here is a single quote <'>

This program consists of three concatenated quoted strings.  The first
and the third are single-quoted, the second is double-quoted.

   This can be "simplified" to:

     $ awk 'BEGIN { print "Here is a single quote <'\''>" }'
     -| Here is a single quote <'>

Judge for yourself which of these two is the more readable.

   Another option is to use double quotes, escaping the embedded,
`awk'-level double quotes:

     $ awk "BEGIN { print \"Here is a single quote <'>\" }"
     -| Here is a single quote <'>

This option is also painful, because double quotes, backslashes, and
dollar signs are very common in `awk' programs.

   If you really need both single and double quotes in your `awk'
program, it is probably best to move it into a separate file, where the
shell won't be part of the picture, and you can say what you mean.


automatically generated by info2www version 1.2.2.9