Ubuntu – Difference between $’$foo’ and $“$foo”

bashcommand linescripts

I am having difficulties figuring out some the nuances between single and double quotes within a variable context

I define:

foo=pwd

then run these:

echo $'$foo'

Which echos $foo (meaning the first $ in my echo command is dropped)

echo $"$foo"

This echos pwd (which means the bash expands $foo, my variable, to its value)

echo $`$foo`

Finally this echos $~/scripts (I expected it to print ~/scripts and not $~/scripts)

can somebody help me figure out these differences?

Best Answer

Ultimately, the last one prints $~/scripts because $`foo` is a combination of a literal $ followed by a command substitution; so the leading $ is interpreted as a literal $ and the trailing `$foo` as a command substitution.