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

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}'

I’m Rich & Co.

Welcome to Tongfamily, our cozy corner of the internet dedicated to all things technology and interesting. Here, we invite you to join us on a journey of tips, tricks, and traps. Let’s get geeky!

Let’s connect