Thomas' Tech Tips

How to escape single quotes with awk

9 August 2022 - Thomas Damgaard

If you have tried printing a single quote (') with awk, you might have found that using single quotes with awk can be a bit tricky. This is due to the fact that ' is a shell string literal. A regular escape using backslash does not work.

It can be done with '\''.


awk '{ print "'\''" $2 "'\''" }' 

This will output the content of $2 surrounded by single quotes.

'\'' works like this:

  1. The first ' closes the opening single quote.
  2. Then \' prints a literal ' by escaping it.
  3. The final ' opens the single quote again.
Filed under: awk, howto, shell, tips

Back to article list