How do I use variables in single quoted strings?
How do I use variables in single quoted strings?
I am just wondering how I can echo a variable inside single quotes (I am using single quotes as the string has quotation marks in it).
echo 'test text "here_is_some_test_text_$counter" "output"' >> $FILE
any help would be greatly appreciated
See Difference between single and double quotes in Bash as well.
– codeforester
Sep 13 '18 at 21:53
7 Answers
7
Variables are expanded in double quoted strings, but not in single quoted strings:
$ name=World
$ echo "Hello $name"
Hello World
$ echo 'Hello $name'
Hello $name
If you can simply switch quotes, do so.
If you prefer sticking with single quotes to avoid the additional escaping, you can instead mix and match quotes in the same argument:
$ echo 'single quoted. '"Double quoted. "'Single quoted again.'
single quoted. Double quoted. Single quoted again.
$ echo '"$name" has the value '"$name"
"$name" has the value World
Applied to your case:
echo 'test text "here_is_some_test_text_'"$counter"'" "output"' >> "$FILE"
Alternatively,
echo "test text "here_is_some_test_text_$counter" "output""
... Escape the double quotes you don't want the shell to interpret.– twalberg
Jan 17 '14 at 18:03
echo "test text "here_is_some_test_text_$counter" "output""
Don't forget that you have to quote
"$FILE"
.– Aleks-Daniel Jakimenko-A.
Jan 18 '14 at 4:15
"$FILE"
@Aleks-DanielJakimenko-A. Is it necessary?
– Joshua Detwiler
Jul 2 '18 at 20:24
@JoshDetwiler Long story short: yes. The answer you linked is fine and goes into all the details, but quoting a variable never hurts and most often quotes are indeed required for proper behavior.
– Aleks-Daniel Jakimenko-A.
Jul 3 '18 at 21:02
use printf:
printf 'test text "here_is_some_test_text_%s" "output"n' "$counter" >> $FILE
Please quote
"$FILE"
.– Aleks-Daniel Jakimenko-A.
Jan 18 '14 at 4:14
"$FILE"
Use a heredoc:
cat << EOF >> $FILE
test text "here_is_some_test_text_$counter" "output"
EOF
The most readable, functional way uses curly braces inside double quotes.
'test text "here_is_some_test_text_'"$counter"'" "output"' >> "$FILE"
Duplicate of Ignacio Vazquez-Abrams's answer from 10 months back
– Jonas Berlin
Oct 14 '16 at 22:00
@JonasBerlin: Not exactly a duplicate, but, given that the alleged improvement is incidental to making the original solution work, this should be a comment, not an answer.
– mklement0
Oct 14 '16 at 22:06
Unfortunately I do not have the reputation to leave a comment.
– Paul Back
Oct 17 '16 at 17:55
You can do it this way:
$ counter=1 eval echo `echo 'test text
"here_is_some_test_text_$counter" "output"' |
sed -s 's/"/\\"/g'` > file
cat file
test text "here_is_some_test_text_1" "output"
Explanation:
Eval command will process a string as command, so after the correct amount of escaping it will produce the desired result.
It says execute the following string as command:
'echo test text "here_is_some_test_text_$counter" "output"'
Command again in one line:
counter=1 eval echo `echo 'test text "here_is_some_test_text_$counter" "output"' | sed -s 's/"/\\"/g'` > file
Output a variable wrapped with single quotes:
printf "'"'Hello %s'"'" world
Doesn't work if your variable contains literal single quotes, so this isn't reliable with unknown values.
– Charles Duffy
Jan 31 at 20:55
with a subshell:
var='hello' echo 'blah_'`echo $var`' blah blah';
Does not work, echoes
blah_`echo $var` blah blah
– Jonas Berlin
Oct 14 '16 at 21:57
blah_`echo $var` blah blah
You're right, needs to be surrounded by double quotes instead of simple quotes. I fixed the answer.
– R.Sicart
Oct 18 '16 at 14:40
Your new answer will compress any whitespace in
$var
.. please see Ignacio Vazquez-Abrams' answer..– Jonas Berlin
Oct 19 '16 at 19:04
$var
That's a useless use of
echo
. You'll be fine with 'blah_'"$var"' blah blah.'
But that's already in Ignacio's answer.– tripleee
Jul 2 '18 at 20:17
echo
'blah_'"$var"' blah blah.'
Thanks for contributing an answer to Stack Overflow!
But avoid …
To learn more, see our tips on writing great answers.
Required, but never shown
Required, but never shown
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
See also stackoverflow.com/questions/10067266/…
– tripleee
Jul 15 '18 at 19:06