Bash – ny way to print value inside variable inside single quote

bashquotingshellvariable substitution

Consider I've set variable site and needs to be printed by echo or printf, but If I use single quote to write something and want to use variable then how?

Example:

$ site=unix.stackexchange.com
$ echo "visit:$site"
visit:unix.stackexchange.com

But If I use single quote:

$ echo 'visit:$site'
visit:$site

Then we know that '' is strong quote and will not expand the variable

I've tried something:

$ echo 'visit:"$site"'
visit:"$site"

but do not succeed. So, I am looking for way to print value inside variable while using single quote.

Best Answer

You can't expand variables in single quotes. You can end single quotes and start double quotes, though:

echo 'visit:"'"$site"'"'

Or, you can backslash double quotes inside of double quotes:

echo "visit:\"$site\""
Related Question