echo Command Guide
echo writes a line of text to standard output — or, more usefully, to whatever you redirect it to. It’s the simplest printing tool in Linux and a constant in shell scripts: printing messages, writing config lines, and debugging variable values.
What is echo, and Why Use It?
You want to:
- print a message to the screen,
- write a line into a file,
- check what a variable actually contains inside a script.
echo does all three in the fewest characters possible:
echo "Hello, world!"
echo "The server is up." >> /var/log/deploy.log
echo "HOME is $HOME"
It’s also the classic way to create a one-line file:
echo "server.port=8080" > app.conf
Basic Usage
# Print a line (with a trailing newline)
echo hello
# Print with spaces and punctuation — quote it
echo "Two spaces preserved"
# Print a variable's value
NAME="cli-wiki"
echo "Building $NAME"
# Print multiple things on one line
echo "count:" 42 "status: ok"
Quotes matter: unquoted $NAME undergoes word splitting and globbing
before echo ever sees it. When in doubt, quote.
Escapes and the -e Flag
By default, most shells’ echo prints backslash sequences literally. The
-e flag tells it to interpret escapes:
echo -e "Line 1\nLine 2" # two lines
echo -e "Col1\tCol2" # a tab between them
echo -e "\e[31mred\e[0m" # ANSI colors work too
Without -e:
echo "Line 1\nLine 2" # prints the literal \n
Note that -e is a bash/zsh extension, not POSIX — see the pitfalls.
No Trailing Newline: -n
echo appends a newline unless you pass -n:
echo -n "Enter your name: " # prompt without pressing Enter yourself
read -r name
echo -n "$name" > username.txt
This is why echo composes well with printf prompts and
with read in scripts.
echo and Redirection
echo’s real power shows up when you point it somewhere:
# Overwrite a file
echo "127.0.0.1 myhost" > /etc/hosts.d/mine
# Append a line
echo "$(date) deploy ok" >> /var/log/deploy.log
# Feed a pipeline
echo "alpha beta gamma" | tr ' ' '\n'
See redirection for the full >,
>>, and < model, and pipes for
connecting echo to other commands.
echo in Scripts
#!/bin/bash
set -euo pipefail
for host in web1 web2 web3; do
echo "Checking $host..."
# ... do something ...
done
echo "Done. Processed ${#hosts[@]} hosts."
echo is also the standard debugging move — print the variable, run the script again:
echo "DEBUG: target=$TARGET dry_run=$DRY_RUN"
echo vs printf
They overlap, but they’re not interchangeable:
echois simpler but its escape handling (-e) and newline behavior vary between shells and implementations.printfis POSIX-guaranteed, supports formatting, and never guesses.
When a script needs to be portable, or when the text contains backslashes
or a leading -, prefer printf:
printf '%s\n' "$value" # safe for any value, even "-e" or "a\nb"
Common Pitfalls
echo -eisn’t portable. dash (the default/bin/shon Debian) and some minimal shells don’t support-e— they print it as literal text. In portable scripts useprintfinstead.- Values starting with
-.echo "-n"prints nothing useful — echo reads-nas its own flag. Useecho -- "-n"orprintf '%s\n' "-n". - Unquoted variables.
echo $listcollapses whitespace and expands globs;echo "$list"prints the value as-is. - Backslash surprises. On some systems
echo "a\nb"interprets the escape even without-e(bash’sxpg_echooption,/bin/echoon others). If escapes matter, be explicit — or switch to printf. - Overwriting by accident.
echo x > filetruncates the file first. Use>>to append; see redirection.
Related Pages
- printf — the precise, portable formatter
- redirection — sending echo’s output to files
- pipes — chaining echo into other commands
- scripting — echo inside real scripts
- tr — transforming echo’d text
Cheatsheet
| Command | What it does |
|---|---|
echo hello | Print a line |
echo "a b" | Preserve spacing inside quotes |
echo -e "a\nb" | Interpret backslash escapes |
echo -n "prompt: " | Print without trailing newline |
echo "$VAR" | Print a variable’s value safely |
echo "line" >> file | Append a line to a file |
echo -- "-n" | Print text that looks like a flag |
printf '%s\n' "$x" | The portable alternative |