26

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.

1

6 Answers 6

48

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\""
1
  • I like the suggestion of replacing single quotes with double quotes, and escaping them when they need to be there literally. For me, this reads more easily than other suggestions. Thank you.
    – Craig
    Commented Dec 31, 2021 at 22:44
13

When you deal with printing variable content, you should stick with printf instead of echo:

printf 'visit:%s\n' "$site"

will output visit: followed by content of $site and a newline regardless of characters in $site.

2

I’m assuming you want the output

visit:'unix.stackexchange.com'

While Stephen’s answer is dangerous, it suggests this approach:

$ cat << EOF
> visit:'$site'
> EOF
1

If I understand your request right, you can eval(uate) the echoed string. E.g.:

# eval echo 'visit:$site'
visit:unix.stackexchange.com
2
  • 1
    It appears that what's asked for is the output 'visit:unix.stackexchange.com', with single quotes.
    – Kusalananda
    Commented Jan 23, 2018 at 11:46
  • 2
    This is exactly what I was looking for, thank you @drgnfr ! Full demonstration: cd /var && QUOTED='cd $HOME' && echo $QUOTED && $(eval echo "$QUOTED") -- first command moves you to a specific dir, at the end you will be in your HOME dir
    – Rogus
    Commented Mar 28, 2020 at 18:08
0

when concatenating a string and a variable the variable has to be outside of the quotation marks as anything between the quotation marks will be printed literally.

echo 'visit:'$site

If you wanted to continue the string after the variable you would do the same:

echo 'visit: '$site' rest of the sentence'
1
  • 4
    That's a really bad approach, I'm sad to say. Always double-quote your variables (unless you really know exactly when you don't need to do so) so that the shell doesn't break the values on whitespace. In your example you should have echo "visit:$site" rather than echo 'visit:'$site, and echo "visit: '$site' rest of sentence" instead of echo 'visit: '$site' rest of the sentence'. If your fixed text contains a quote or other special character you can either jump in and out of quotes or escape them, eg echo '"Look out", said '"$person" or echo "\"Look out\", said $person" Commented Jun 18, 2020 at 15:17
-1

choroba is correct.

Though if this were some sort of riddle, I'd respond with this:

cat << EOF | sh
> echo 'visit:$site'
> EOF

As jander points out this is wide open for an injection attack. It wasn't a serious answer, so if anyone was considering using something like this, don't use it with untrusted input. For example, validate the $site string as being a valid URL before blindly executing the content. Something like this could help (but the expression provided isn't perfect because it still allows injection, but use that sort of mechanic to test it).

2
  • I think EOF (itself) working as (another) single quote (something like: bash -c 'echo 'visit:$site'')!
    – Pandya
    Commented Jun 16, 2015 at 12:31
  • 5
    I hope $site doesn't contain '\nrm -rf /\necho '!
    – Jander
    Commented Jun 16, 2015 at 17:50

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .