1 1 1.1.6 Shell Quoting Issues 1 -------------------------- 1
1 · DOS Quoting Quoting in Windows Batch Files. 1 1 For short to medium-length 'awk' programs, it is most convenient to 1 enter the program on the 'awk' command line. This is best done by 1 enclosing the entire program in single quotes. This is true whether you 1 are entering the program interactively at the shell prompt, or writing 1 it as part of a larger shell script: 1 1 awk 'PROGRAM TEXT' INPUT-FILE1 INPUT-FILE2 ... 1 1 Once you are working with the shell, it is helpful to have a basic 1 knowledge of shell quoting rules. The following rules apply only to 1 POSIX-compliant, Bourne-style shells (such as Bash, the GNU Bourne-Again 1 Shell). If you use the C shell, you're on your own. 1 1 Before diving into the rules, we introduce a concept that appears 1 throughout this Info file, which is that of the "null", or empty, 1 string. 1 1 The null string is character data that has no value. In other words, 1 it is empty. It is written in 'awk' programs like this: '""'. In the 1 shell, it can be written using single or double quotes: '""' or ''''. 1 Although the null string has no characters in it, it does exist. For 1 example, consider this command: 1 1 $ echo "" 1 1 Here, the 'echo' utility receives a single argument, even though that 1 argument has no characters in it. In the rest of this Info file, we use 1 the terms "null string" and "empty string" interchangeably. Now, on to 1 the quoting rules: 1 1 * Quoted items can be concatenated with nonquoted items as well as 1 with other quoted items. The shell turns everything into one 1 argument for the command. 1 1 * Preceding any single character with a backslash ('\') quotes that 1 character. The shell removes the backslash and passes the quoted 1 character on to the command. 1 1 * Single quotes protect everything between the opening and closing 1 quotes. The shell does no interpretation of the quoted text, 1 passing it on verbatim to the command. It is _impossible_ to embed 11 a single quote inside single-quoted text. Refer back to ⇒ Comments for an example of what happens if you try. 1 1 * Double quotes protect most things between the opening and closing 1 quotes. The shell does at least variable and command substitution 1 on the quoted text. Different shells may do additional kinds of 1 processing on double-quoted text. 1 1 Because certain characters within double-quoted text are processed 1 by the shell, they must be "escaped" within the text. Of note are 1 the characters '$', '`', '\', and '"', all of which must be 1 preceded by a backslash within double-quoted text if they are to be 1 passed on literally to the program. (The leading backslash is 1 stripped first.) Thus, the example seen in ⇒Read Terminal: 1 1 awk 'BEGIN { print "Don\47t Panic!" }' 1 1 could instead be written this way: 1 1 $ awk "BEGIN { print \"Don't Panic!\" }" 1 -| Don't Panic! 1 1 Note that the single quote is not special within double quotes. 1 1 * Null strings are removed when they occur as part of a non-null 1 command-line argument, while explicit null objects are kept. For 1 example, to specify that the field separator 'FS' should be set to 1 the null string, use: 1 1 awk -F "" 'PROGRAM' FILES # correct 1 1 Don't use this: 1 1 awk -F"" 'PROGRAM' FILES # wrong! 1 1 In the second case, 'awk' attempts to use the text of the program 1 as the value of 'FS', and the first file name as the text of the 1 program! This results in syntax errors at best, and confusing 1 behavior at worst. 1 1 Mixing single and double quotes is difficult. You have to resort to 1 shell quoting tricks, like this: 1 1 $ awk 'BEGIN { print "Here is a single quote <'"'"'>" }' 1 -| Here is a single quote <'> 1 1 This program consists of three concatenated quoted strings. The first 1 and the third are single-quoted, and the second is double-quoted. 1 1 This can be "simplified" to: 1 1 $ awk 'BEGIN { print "Here is a single quote <'\''>" }' 1 -| Here is a single quote <'> 1 1 Judge for yourself which of these two is the more readable. 1 1 Another option is to use double quotes, escaping the embedded, 1 'awk'-level double quotes: 1 1 $ awk "BEGIN { print \"Here is a single quote <'>\" }" 1 -| Here is a single quote <'> 1 1 This option is also painful, because double quotes, backslashes, and 1 dollar signs are very common in more advanced 'awk' programs. 1 11 A third option is to use the octal escape sequence equivalents (⇒ Escape Sequences) for the single- and double-quote characters, like 1 so: 1 1 $ awk 'BEGIN { print "Here is a single quote <\47>" }' 1 -| Here is a single quote <'> 1 $ awk 'BEGIN { print "Here is a double quote <\42>" }' 1 -| Here is a double quote <"> 1 1 This works nicely, but you should comment clearly what the escapes mean. 1 1 A fourth option is to use command-line variable assignment, like 1 this: 1 1 $ awk -v sq="'" 'BEGIN { print "Here is a single quote <" sq ">" }' 1 -| Here is a single quote <'> 1 1 (Here, the two string constants and the value of 'sq' are 1 concatenated into a single string that is printed by 'print'.) 1 1 If you really need both single and double quotes in your 'awk' 1 program, it is probably best to move it into a separate file, where the 1 shell won't be part of the picture and you can say what you mean. 1