What is the difference between single quoted $'string' and double quoted $"string" in bash?

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).


The $ in the beginning of the string in :

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

causes escape sequences to be interpreted.

Bash reference manual [ says ]

Words of the form $'string' are treated specially. The word expands to string, with backslash-escaped characters replaced as specified by the ANSI C standard. ..
..
The expanded result is single-quoted, as if the dollar sign had not been present.

But

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

is completely different. This [ article ] on locale specific translation says :

A double-quoted string preceded by a dollar sign (‘$’) will cause the string to be translated according to the current locale. If the current locale is C or POSIX, the dollar sign is ignored. If the string is translated and replaced, the replacement is double-quoted.


Note: IIRC both $'string' and $"string" may not find support among different shells. Not only do people from other shells look at them with curiosity but also they debate on whether this could be avoided for script portability.

Tags:

Bash

Echo

Quoting