The echo command in Linux

echo prints its arguments to standard output. On its own that seems minor, but combined with shell redirection it becomes the quickest way to write text into a file, and inside scripts it is how you print messages and inspect variables. Understanding the difference between > (overwrite) and >> (append) is essential — mixing them up destroys file contents.

How echo works

By the time echo runs, the interesting work is finished. The shell has already expanded variables, substituted $(commands), interpreted quotes and exploded globs; echo merely prints the argument list it received, separated by spaces. echo *.txt listing files is the shell's doing — echo never saw a wildcard.

This makes echo the perfect instrument for inspecting what the shell does to a command line before a real program gets it: prefix anything with echo and you see the post-expansion truth. It is also why quoting matters so much: echo $var and echo "$var" hand echo different argument lists — the first split on whitespace, the second intact. Most "echo is behaving strangely" reports are actually the shell's expansion rules, working exactly as designed.

Syntax

echo [OPTIONS] [TEXT...]

Common options

OptionWhat it does
-nDo not print the trailing newline.
-eInterpret escapes: \n newline, \t tab, \\ backslash.
> fileRedirection (shell, not echo): write output to a file, replacing its contents.
>> fileRedirection: append output to the end of a file, preserving its contents.

How to use echo: examples

$ echo "Hello, world"

Print a line of text.

$ echo $HOME

Print the value of an environment variable.

$ echo "backup complete" > status.txt

Write the text into status.txt, replacing whatever was there.

$ echo "new entry" >> log.txt

Append a line to the end of log.txt without touching existing content.

$ echo -e "line 1\nline 2"

With -e, \n becomes a real newline, printing two lines.

$ echo *.txt

The shell expands the wildcard first, so this prints the matching filenames — a quick trick to preview a glob.

Real-world use cases for echo

Debugging shell variables

A script misbehaves and you suspect a variable: echo "PATH=[$PATH]" or echo "count=[$count]" shows exactly what the shell sees, brackets exposing stray whitespace. Sprinkling echos remains the fastest bash debugger ever shipped.

Appending to config from scripts

Provisioning scripts add lines idempotently: grep -q "alias ll" ~/.bashrc || echo "alias ll='ls -lah'" >> ~/.bashrc — check if the line exists, append only if missing. The echo >> pattern powers half the dotfile installers on GitHub.

Try echo yourself

This is a live sandbox with a small filesystem (documents/, notes.txt, backup.sh…). Nothing you do here can break anything — experiment freely. Try: echo "Hello" > hello.txt

Pro tips and common mistakes

Frequently asked questions about echo

What is the difference between > and >>?

> truncates the file and writes from scratch — existing content is lost. >> appends to the end, preserving what is there. When in doubt, >> is the safe one.

How do I write multiple lines to a file?

Either echo -e "one\ntwo" > file, or better, a heredoc: cat > file << EOF, then your lines, then EOF on its own line.

Why does echo $VAR print nothing?

The variable is unset or empty in the current shell. Check with printenv VAR, and remember variables set in a script do not persist after it exits unless exported and sourced.

Related commands

touch

Create empty files instantly, or update the timestamps of existing ones.

cat

Print file contents, number lines, and join multiple files together.

grep

Find lines matching a pattern in files or piped input — the workhorse of text search.

Want to build real fluency? The interactive course takes you through echo and every other essential command with guided, checked exercises.