What is the defference between using
echo -e "Hello\nWorld"
and
echo $"Hello\nWorld"
don't they both output:
Hello
World
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It only takes a minute to sign up.
Sign up to join this communityecho -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.
$"..."
By contrast with $'...'
, the function of $"..."
is quite different. It will cause the string it contains to be translated according to the current locale.
echo -e
controversyecho -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.
*
) isn't actually specific to echo
. It happens with any command that recognizes short or long options (written in the traditional way) and takes filename operands, including ls
. Arguably the situation is a little worse with echo
since (as your source mentions) echo
doesn't typically accept --
to indicate the end of options, whereas one can write commands like ls -- *l
. (And really one ought usually to write commands that way, when using globs that begin with *
or -
, especially in scripts.)
Oct 17, 2014 at 8:55
\n
escape sequence, so in fact it doesn't matter if I use echo -e '...'
, echo $'...'
, or echo $"..."
Hello\nWorld
. You need the-e
switch to interpret escape characters.echo $'Hello\nWorld'
, which will print out on two lines under bash.