How do I use variables in single quoted strings?

Multi tool use
Multi tool use


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




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?
– Josh Detwiler
2 days ago





@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.
yesterday



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



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
2 days ago



echo


'blah_'"$var"' blah blah.'



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






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.

5iYhdzvekuj MGG9zxex,trL7fpVfb,m5rO1nMuH SI1,9UNUDK5BkZurz4yCH VKnaSAamh,B ipgkd8hBYod111
iMQ8Y WnjVoI 5F sml Y y3,CeXZCme0NRC0AFEemTu07Fx,cqSU

Popular posts from this blog

PySpark - SparkContext: Error initializing SparkContext File does not exist

django NoReverseMatch Exception

List of Kim Possible characters