echo — printing text
Printing strings and variables; -n and -e, escaping, when to prefer printf.
Cheat sheet
5 commands Printable version-
echo "x=$var"with variable expansion -
echo 'literal $x'without expansion -
echo -n / echo -eno trailing \n / with escapes -
echo "x" >> fileappend to a file -
printf '%s\n' "$v"a portable equivalent
Overview
echo prints its arguments to stdout, separated by spaces, with a trailing
newline. It's the main tool for "show a value" and "write a line to a file".
Options
3Rule of thumb: variables and special characters go in double quotes; fully literal text goes in single quotes. More on expansion — globbing and env-vars.
| Option | What it does | Example |
|---|---|---|
-n |
Do not append a trailing newline. | echo -n "no newline" |
-e |
Enable interpretation of backslash escapes (\n, \t, \\). | echo -e "col1\tcol2" |
-E |
Explicitly disable backslash escapes (the default in bash). | echo -E "literal\ttext" |
Nothing found. Try a shorter query.
Recipes
2When to use printf
echo behaves differently across shells (especially -e/-n). For scripts
and exact formatting, use printf — it's portable and predictable:
Troubleshooting
3-e printed as literal text
The shell isn't bash (behavior differs in sh/dash), or echo is an
external binary. Use printf instead.
An asterisk turned into a file list
echo * without quotes is subject to globbing. Quote it: echo "*".
The newline isn't where I want it
echo always adds a trailing \n; to skip it, use -n; to insert one in the
middle, use -e "...\n..." or printf.
See also
- printf — Formatted output in the shell — more reliable than echo, formats and escapes.
- env-vars — env, export, $PATH, ~/.bashrc vs profiles; why "command not found".
- bash-scripting — Variables, conditionals, loops, arguments, and the safe mode set -euo pipefail.
Found an inaccuracy? Report an error in the article