Difference between single and double quotes in awk

后端 未结 3 1457
灰色年华
灰色年华 2020-12-05 16:39

I have this awk statement:

glb_library=\"my_library\"
awk \"
        /^Direct Dependers of/ { next }
        /^---/                 { next }
            


        
3条回答
  •  佛祖请我去吃肉
    2020-12-05 17:27

    A pragmatic summary:

    • As Ed Morton's helpful answer sensibly recommends:
      Always use single quotes to enclose your awk script as a whole ('...'), which ensures that there's no confusion over what the shell interprets up front, and what awk ends up seeing.

    • To define strings inside an awk script, always use double quotes ("...").

      • " is the only string delimiter awk recognizes.
      • "..." strings are non-interpolating (you cannot embed variable references), but they do recognize control-character sequences such as \n and \t.
    • A single quote (') has no syntactic meaning inside an awk script, but, - if you're using '...' for your overall script, as recommended - you cannot use a literal ' inside of it anyway, because the shell's single-quoted strings do not permit embedded ' chars.

      • If you do need to use a literal single quote (') in your awk script, you have three choices:
        • Pass a variable that defines it, and use awk's string concatenation, based on directly adjoining string literals and variable references:
          awk -v q=\' 'BEGIN { print "I" q "m good." }' # -> I'm good
        • Use an escape sequence inside "..."; for maximum portability and disambiguation, use an octal escape sequence (\047), not a hex one (\x27):
          awk 'BEGIN { print "I\047m good." }' # -> I'm good
        • Use '\'' (sic) to "escape" embedded ' chars. (technically, 3 distinct single-quoted shell string literals are being concatenated)Thanks, snr:
          awk 'BEGIN { print "I'\''m good" }' # -> I'm good

提交回复
热议问题