44

How do I echo a variable inside single quotes?

echo 'test text "here_is_some_test_text_$counter" "output"' >> ${FILE}
Mateen Ulhaq
  • 24,552
  • 19
  • 101
  • 135
Pectus Excavatum
  • 3,593
  • 16
  • 47
  • 68

8 Answers8

77

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"
Aleks-Daniel Jakimenko-A.
  • 10,335
  • 3
  • 41
  • 39
Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
  • 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
  • *If you can simply switch quotes* not possible if you're using quote-surrunded JSON (JSON requires double quotes for its values) – DarkTrick Nov 22 '22 at 00:32
7

use printf:

printf 'test text "here_is_some_test_text_%s" "output"\n' "$counter" >> ${FILE}
glenn jackman
  • 238,783
  • 38
  • 220
  • 352
4

Use a heredoc:

cat << EOF >> ${FILE}
test text "here_is_some_test_text_$counter" "output"
EOF
William Pursell
  • 204,365
  • 48
  • 270
  • 300
3

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

'test text "here_is_some_test_text_'"${counter}"'" "output"' >> "${FILE}"
Paul Back
  • 1,269
  • 16
  • 23
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
  • 683
  • 6
  • 8
0

Adding another pair of single quotes arround the variable solved my problem.

For your case:

echo 'test text "here_is_some_test_text_'$counter'" "output"' >> ${FILE}

DarkTrick
  • 2,447
  • 1
  • 21
  • 39
-1

with a subshell:

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