Bash Quoting – Difference Between Single Quoted $’string’ and Double Quoted $”string” in Bash

bashechoquoting

I was trying to execute new line using echo and tried following two commands:

  1. First command:

    echo $'Hello World\nThis is a new line'
    

    Response:

    Hello World
    This is a new line
    
  2. Second command:

    echo $"Hello World\nThis is a new line"
    

    Response:

    Hello World\nThis is a new line
    

My question is what's the difference between string wrapped with $' ' vs string wrapped with $" " in bash's echo?

Best Answer

As explained here, the syntax $'string' specifies a C-style string which includes magic escaped characters, such as \n for a newline. $"string" is for I18N expansion, which has no such magic escapes.

Note that these are distinct from the more common "string" (weak quoting) and 'string' (strong quoting).

Related Question