15

What is the defference between using

echo -e "Hello\nWorld" 

and

echo $"Hello\nWorld" 

don't they both output:

Hello
World
  • 1
    Did you try it? The second one outputs Hello\nWorld. You need the -e switch to interpret escape characters. – becko Oct 16 '14 at 21:47
  • 4
    For the second one, I suspect that you meant to write it with single quotes, echo $'Hello\nWorld', which will print out on two lines under bash. – John1024 Oct 16 '14 at 21:50
  • @becko, yes I tried it and they both output the same exact thing. – Mark Oct 18 '14 at 12:22
27

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.

  • 1
    Also worth noting that both Chet and POSIX recommend using printf rather than echo – geirha Oct 17 '14 at 7:26
  • @geirha Very good points. Answer updated. – John1024 Oct 17 '14 at 7:34
  • 1
    The problem of unexpected options from globs (like *) 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.) – Eliah Kagan Oct 17 '14 at 8:55
  • Thank you for the thorough answer, I have an excercise for my Operating Systems course and I needed to print out some text using the \n escape sequence, so in fact it doesn't matter if I use echo -e '...', echo $'...', or echo $"..." – Mark Oct 18 '14 at 12:27

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.