33

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

Xavi
  • 19,751
  • 14
  • 71
  • 63
Pectus Excavatum
  • 3,323
  • 11
  • 44
  • 64

7 Answers7

66

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"
Ignacio Vazquez-Abrams
  • 740,318
  • 145
  • 1,296
  • 1,325
  • 2
    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
  • 5
    Don't forget that you have to quote ``"$FILE"``. – Aleks-Daniel Jakimenko-A. Jan 18 '14 at 04:15
  • @Aleks-DanielJakimenko-A. [Is it necessary?](https://unix.stackexchange.com/questions/68694/when-is-double-quoting-necessary/68748#68748) –  Jul 02 '18 at 20:24
  • 1
    @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 03 '18 at 21:02
7

use printf:

printf 'test text "here_is_some_test_text_%s" "output"\n' "$counter" >> ${FILE}
glenn jackman
  • 223,850
  • 36
  • 205
  • 328
4

Use a heredoc:

cat << EOF >> ${FILE}
test text "here_is_some_test_text_$counter" "output"
EOF
William Pursell
  • 190,037
  • 45
  • 260
  • 285
2

The most readable, functional way uses curly braces inside double quotes.

'test text "here_is_some_test_text_'"${counter}"'" "output"' >> "${FILE}"
Paul Back
  • 1,171
  • 14
  • 22
0

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
0

Output a variable wrapped with single quotes:

printf "'"'Hello %s'"'" world
Elior Malul
  • 649
  • 6
  • 8
-1

with a subshell:

var='hello' echo 'blah_'`echo $var`' blah blah';
R.Sicart
  • 673
  • 3
  • 10