15

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.

3
  • 2
    echo 'visite:"'$site'"'? (or echo "visite:\"$site\"" but it's not single quoted…) – fredtantini Jun 16 '15 at 10:16
  • @fredtantini nope, this also seems to ending single quote first – Pandya Jun 16 '15 at 10:38
  • @choroba has posted an answer very similar to my comment. Quoting variable should be prefered (i.e. 'visit:"'"$site"'"') – fredtantini Jun 16 '15 at 10:38
31

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\""
0
10

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.

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 Jan 23 '18 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 Mar 28 '20 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
  • 2
    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" – roaima Jun 18 '20 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 Jun 16 '15 at 12:31
  • 4
    I hope $site doesn't contain '\nrm -rf /\necho '! – Jander Jun 16 '15 at 17:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.