What is the defference between using
echo -e "Hello\nWorld"
and
echo $"Hello\nWorld"
don't they both output:
Hello
World
1 Answer
echo -e and echo $'...' are both similar in that they support the following escape sequences:
\a alert (bell)
\b backspace
\e
\E an escape character
\f form feed
\n new line
\r carriage return
\t horizontal tab
\v vertical tab
\\ backslash
\0nnn the eight-bit character whose value is the octal value nnn (zero to three octal digits)
\xHH the eight-bit character whose value is the hexadecimal value HH (one or two hex digits)
\uHHHH the Unicode (ISO/IEC 10646) character whose value is the hexadecimal value HHHH (one to four hex digits)
\UHHHHHHHH
the Unicode (ISO/IEC 10646) character whose value is the hexadecimal value HHHHHHHH (one to eight hex digits)
They do have differences. In addition to the above, echo -e supports:
\c suppress further output
\0nnn the eight-bit character whose value is the octal value nnn (zero to three octal digits)
By contrast, $'....' supports:
\' single quote \" double quote \nnn the eight-bit character whose value is the octal value nnn (one to three digits) \cx a control-x character
Observe that, between the two, the \c extensions are incompatible:
$ echo -e 'start\n\cIstop'
start
$ echo $'start\n\cIstop'
start
stop
For echo -e above, \c suppresses further output, thereby ignoring the Istop. By contrast, for $'...', the \cI is interpreted as a tab.
The visually-similar form: $"..."
By contrast with $'...', the function of $"..." is quite different. It will cause the string it contains to be translated according to the current locale.
The echo -e controversy
echo -e is not universally supported by shells and many regard the -e option as a design mistake. Observe:
$ ls
-e -n
$ echo *
$ printf "%s\n" *
-e
-n
As you can see, if what you are printing with echo starts with a dash, the results can be unexpected. Unless you are sure that the first string that you will print with echo does not start with a dash, you are likely better off using printf.
For these reasons, the POSIX standard concludes:
New applications are encouraged to use printf instead of echo.
Chet Ramey, who has maintained bash for the last 22 years, agrees:
[N]ew code should use printf.