girl showing a message against bullying written in a notebook

More Geek: What’s all this then about quotes in bash

0

Ok, this has to be so confusing, but there are times when you are in a bash script and you want some variables to be substituted and some that are not.

The easy case if when you want double quote inside a double quote, this is used when you are going to feed that string to another bash script that you will call, so for instance:

$ WORLD=crazy
$ echo "hello \"\$WORLD\" $WORLD"
hello "$WORLD" crazy

But you can’t do this for a single quote, so the very logical "echo \'\$WORLD\' generates and error. Turns out you need to use the special form which is $"some string" which does allow this back quoting that is ANSI C standard. And as a reminder, the $NF to awk means find the last field in a line, so pretty useful.

$ echo $"awk \'{print $NF}\'"
awk '{print $NF}'

Related Posts

This site uses Akismet to reduce spam. Learn how your comment data is processed.