Skip to content
Tutorials Center
My learning

Topics

Linux Command Reference ยท Text Processing

Linux echo command

Reference for the Linux echo command: print arguments, suppress the newline with -n, interpret escapes with -e, and why printf is safer for portable scripts.

echo writes its arguments to standard output, separated by single spaces and followed by a newline. It is the first command most people learn and the one most often used in scripts, for status messages and for feeding text into pipes.

echo is a Bash builtin, and a separate program exists as well (/bin/echo). Their behavior differs in small ways, and other shells differ more. For anything beyond a simple message, in particular when the text may start with a dash or contain backslashes, prefer printf.

Terminal
echo [-neE] [STRING]...

Try it

Bash The basics

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Bash Escapes, redirects and pitfalls

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Syntax

Terminal
echo [-neE] [ARG]...
Option Description
-n Do not print the trailing newline.
-e Interpret backslash escapes: \n, \t, \\, \a, \b, \e, \r, \0NNN, \xHH, \uHHHH, and \c (stop output).
-E Do not interpret escapes (the default in Bash).

Returns: 0, unless writing fails (for example to a closed pipe), in which case it is non-zero.

Usage notes

  • Quotes control spacing. Unquoted arguments are split on whitespace and glued together with single spaces. Quote variables (echo "$var") to keep their exact content, and to stop a * from expanding to file names.
  • Options are recognized only in a leading position, and a value like -n, -e or -E (alone) is treated as an option. That makes echo "$var" unreliable for unknown data. Use printf '%s\n' "$var".
  • Escapes vary between shells. Bash needs -e; dash (/bin/sh on Debian) interprets escapes by default and has no -e, printing -e literally. xpg_echo shell option changes Bash too. Scripts for sh should use printf.
  • -- is not an option terminator for the builtin: echo -- text prints -- text.
  • Exit status is almost always 0, so echo is no way to test a condition.
  • Trailing newline: use echo -n or printf without \n when building a prompt or a progress line. Piping echo into hash functions adds a newline that changes the digest; use printf or echo -n.
  • Output of echo $(command) collapses newlines in the output of command into spaces, because the unquoted substitution is word-split.
  • /bin/echo is a separate program: its --help and --version options work, and the builtin ignores them.
  • Message to stderr: echo "error" >&2 is the usual way to report problems without polluting stdout.