100

What is the difference between echo and echo -e?

And which quotes ("" or '') should be used with the echo command? i.e: echo "Print statement" or echo 'Print statement'?

Also, what are the available options that can be used along with echo?

Colandus
  • 103
  • 3
Venkatesh
  • 1,141
  • 2
  • 7
  • 8
  • 5
    What shell? `echo -e` is not defined by POSIX and probably varies by shell. – jordanm Mar 12 '15 at 18:28
  • 1
    Does this answer your question? http://unix.stackexchange.com/questions/65803/why-is-printf-better-than-echo – dhag Mar 12 '15 at 18:30
  • 2
    use `man echo` to read the manual for `echo`. – nanny Mar 12 '15 at 18:51
  • echo doesn't execute its arguments as commands, it just ... echos them – Anthon Mar 12 '15 at 20:21
  • Also helpful: https://askubuntu.com/questions/537984/difference-between-echo-e-string-and-echo-string – Jesse Nickles May 24 '22 at 10:01
  • It's safe to say that if they're not running a BSD-based distro (including its relatives like Mac), they're probably using GNU utils (where `echo -e` behaves different than `echo`) - which are the default set of programs on all the popular distros like Ubuntu / Debian / CentOS / Arch. That said, `man echo` is the best answer if they're unsure about their particular distribution. – AmphotericLewisAcid Apr 18 '23 at 20:09

2 Answers2

114

echo by itself displays a line of text. It will take any thing within the following "..." two quotation marks, literally, and just print out as it is. However with echo -e you're making echo to enable interpret backslash escapes. So with this in mind here are some examples

INPUT: echo "abc\n def \nghi" 
OUTPUT:abc\n def \nghi

INPUT: echo -e "abc\n def \nghi"
OUTPUT:abc
 def 
ghi

Note: \n is new line, ie a carriage return. If you want to know what other sequences are recognized by echo -e type in man echo to your terminal.

3kstc
  • 4,616
  • 15
  • 33
  • 49
  • 8
    `echo` is built-in in most shells. `man echo` will like document the `echo` standalone command instead. – Stéphane Chazelas Jul 24 '16 at 14:01
  • 4
    You're describing the behaviour of GNU `echo` or the GNU shell's `echo` when not in Unix conformance mode. Elsewhere, the behaviour will typically be different. – Stéphane Chazelas Jul 24 '16 at 14:03
  • I'd like to encourage the community to upvote this answer with hopes that it will reopen the question. 88-) – 3kstc May 24 '22 at 23:30
16

In most of the SHELL echo cant take escape sequence ( \n \t ). Where as echo -e can

echo -e " This is \n an \t example"

Single quote and double quote are mostly for handling the interpolation issues. You may find more details here, Why is echo ignoring my quote characters?

Govind Kailas
  • 1,331
  • 1
  • 10
  • 16
  • 7
    That's the other way round. In most shells (ksh88, ksh93, pdksh, mksh, Bourne, zsh, dash, bash, and the sh of all Unix compliant systems, some of them like ksh93 or bash only in some environments) `echo` does expand escape sequences. An `echo` implementation that accepts `-e` is not POSIX compliant. – Stéphane Chazelas Jul 24 '16 at 13:59