0

Hi there i am trying to understand why the following does not work:

awk 'BEGIN{ print "it\'s my life" }'

I know that as soon as the first single quote opens before BEGIN, then it searches for a closing single quote that in general it finds it after }. However in my example i want to print somethin with another single quote in it. Because it is going to interrupt the SHELL'S opening and closing of the first and last single quotes, i need to do something, so i escape it and it doesnn't work. I saw this answer How to escape a single quote inside awk but i didn't undertand why. Thanks.

john
  • 49
  • 1
  • 6
  • 7
    Because the shell doesn't support escaping inside single quotes. – Barmar Apr 16 '21 at 22:02
  • But note that `bash` does provide a nice mechanism: `awk $'BEGIN{ print "it\'s my life" }'` Just precede the opening `'` with `$` to enable the escaping you want. – William Pursell Apr 16 '21 at 22:05
  • 1
    @WilliamPursell But you have to be careful, because then it will expand escape sequences like `\n`, which should normally be passed literally to `awk`, so you have to write `\\n` – Barmar Apr 16 '21 at 22:06
  • @WilliamPursell You could post that as an answer at the linked question. – Barmar Apr 16 '21 at 22:08
  • Thank you very much for your time, i got it!! – john Apr 16 '21 at 22:08
  • @Barmar I would consider posting that as an answer there except that a) that question does not have a bash tag and b) this style of quoting is an abomination! – William Pursell Apr 16 '21 at 22:10
  • What didn't you understand about the answers to the question you link to? – Benjamin W. Apr 16 '21 at 22:43
  • 1
    @john notice that `awk` is not relevant here: `echo 'BEGIN{ print "it\'s my life" }'` (this statement does the same) – Diego Torres Milano Apr 16 '21 at 23:42

1 Answers1

0

My two preferred options:

⒈ Use double quotes for the enclosure

awk "BEGIN{ print 'it\'s my life' }"

⒉ Use a heredoc

awk <<-DONE
BEGIN{
    print "it's my life"
}
DONE
CJK
  • 5,732
  • 1
  • 8
  • 26