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.
echo [-neE] [STRING]...
Try it
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.
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
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,-eor-E(alone) is treated as an option. That makesecho "$var"unreliable for unknown data. Useprintf '%s\n' "$var". - Escapes vary between shells. Bash needs
-e;dash(/bin/shon Debian) interprets escapes by default and has no-e, printing-eliterally.xpg_echoshell option changes Bash too. Scripts forshshould useprintf. --is not an option terminator for the builtin:echo -- textprints-- text.- Exit status is almost always 0, so
echois no way to test a condition. - Trailing newline: use
echo -norprintfwithout\nwhen building a prompt or a progress line. Pipingechointo hash functions adds a newline that changes the digest; useprintforecho -n. - Output of
echo $(command)collapses newlines in the output ofcommandinto spaces, because the unquoted substitution is word-split. /bin/echois a separate program: its--helpand--versionoptions work, and the builtin ignores them.- Message to stderr:
echo "error" >&2is the usual way to report problems without polluting stdout.