ShellQuoting

Quoting can be such a headache for the novice, in shell programming, and especially in awk.

Art Povelones posted a long tutorial on shell quoting on 1999/09/30 which is probably too much detail to repeat with the FAQ; if you could use it, search via <http://groups.google.com/>.

Tim Maher offered his <http://www.consultix-inc.com/quoting.txt>.

This approach is probably the best, and easiest to understand and maintain, for most purposes: (the '@@' is quoted to ensure the shell will copy verbatim, not interpreting environment variable substitutions etc.)

    cat <<'@@' > /tmp/never$$.awk
    { print "Never say can't." }
    @@
    awk -f /tmp/never$$.awk; rm /tmp/never$$.awk

If you enjoy testing your shell's quoting behavior frequently, you could try these:

   (see below for a verbose explanation of the first one, with 7 quotes)

    awk 'BEGIN { q="'"'"'";print "Never say can"q"t."; exit }'
    nawk -v q="'" 'BEGIN { print "Never say can"q"t."; exit }'
    awk 'BEGIN { q=sprintf("%c",39); print "Never say can"q"t."; exit }'
    awk 'BEGIN { q=sprintf("%c",39); print "Never say \"can"q"t.\""; exit }'

However, you would also have to know why you could not use this:

    awk 'BEGIN { q="\'"; print "Never say \"can"q"t.\""; exit }'

explanation of the 7-quote example:

note that it is quoted three different ways:

    awk 'BEGIN { q="'
                     "'"
                        '";print "Never say can"q"t."; exit }'

and that argument comes out as the single string (with embedded spaces)

    BEGIN { q="'";print "Never say can"q"t."; exit }

which is the same as

    BEGIN { q="'"; print "Never say can" q "t."; exit }
                          ^^^^^^^^^^^^^  ^  ^^
                          |           |  |  ||
                          |           |  |  ||
                          vvvvvvvvvvvvv  |  ||
                          Never say can  v  ||
                                         '  vv
                                            t.

which, quite possibly with too much effort to be worth it, gets you

                          Never say can't.