Although this Web page generally only worries about POSIX systems and the POSIX shell, the following issue arises often enough for many users that it is worth addressing.
The “shells” on Microsoft Windows systems use the double-quote character for quoting, and make it difficult or impossible to include an escaped double quote character in a command-line script. The following example, courtesy of Jeroen Brink, shows how to escape the double quotes from this one liner script that prints all lines in a file surrounded by double quotes:
{ print "\"" $0 "\"" }
In an MS-Windows command-line the one-liner script above may be passed as follows:
gawk "{ print \"\042\" $0 \"\042\" }" file
In this example the ‘\042’ is the octal code for a double quote;
gawk
converts it into a real double-quote for output by
the print
statement.
In MS-Windows escaping double quotes is a little tricky because you use backslashes to escape double quotes, but backslashes themselves are not escaped in the usual way; indeed they are either duplicated or not, depending upon whether there is a subsequent double quote. The MS-Windows rule for double-quoting a string is the following:
So to double-quote the one-liner script ‘{ print "\"" $0 "\"" }’ from the previous example you would do it this way:
gawk "{ print \"\\\"\" $0 \"\\\"\" }" file
However, the use of ‘\042’ instead of ‘\\\"’ is also possible and easier to read, because backslashes that are not followed by a double quote don’t need duplication.