0

What are the differences between the three following echo commands?

var='You'
echo "Hello\nThere\n${var}"

Output:

Hello\nThere\nYou

echo 'Hello\nThere\n${var}'

Output:

Hello\nThere\n${var}

echo $'Hello\nThere\n${var}'

Output:

Hello

There

${var}

I understand that single ticks (i.e., '') tells echo to interpret all characters within as literals.

But I was expecting the double quotes to print the newlines, but it didn't. And I don't understand why $'' did print the new lines.

What are the differences between these commands?

Fred
  • 335
  • 3
    Side note about "I understand that single ticks tells echo …": not echo, they tell the shell. Quotes are meaningful to the shell before echo even starts. (The fact echo is a builtin in many shells does not matter). – Kamil Maciorowski Nov 30 '23 at 15:34
  • @KamilMaciorowski You are correct, it appears that my question is a duplicate. In my searching it did not occur to me that the ticks and quotes are at the shell level. – Fred Nov 30 '23 at 15:39
  • echo $'Hello\nThere\n'$var – admstg Nov 30 '23 at 16:46

0 Answers0