Skip to content

echo Command Cheat Sheet

echo is one of the most frequently used commands in Linux shell scripting. It prints arguments to the standard output, often used for displaying status messages, debugging variables, or generating files.


Synopsis

echo [SHORT-OPTION]... [STRING]...

Basic Usage

echo "Hello, World!"
USER="Alice"
echo "Hello, $USER"
echo ""

Options

Disable Trailing Newline (-n)

By default, echo adds a newline character at the end. Use -n to suppress it.

echo -n "Processing..."
# Output: Processing... (cursor remains on same line)

Interpret Backslash Escapes (-e)

Enable interpretation of backslash escapes (like \n, \t, \c).

echo -e "Line 1\nLine 2"

Disable Backslash Interpretation (-E)

Strictly print backslashes as literal characters (default behavior in some shells).

echo -E "Use \n for newline"

Escape Sequences (requires -e)

Sequence Description
\\ Backslash
\a Alert (BEL)
\b Backspace
\c Produce no further output
\e Escape
\f Form feed
\n New line
\r Carriage return
\t Horizontal tab
\v Vertical tab

Example: Tab-Separated Output

echo -e "Name\tAge\tCity"
echo -e "Alice\t30\tParis"

Example: Progress Indicator (-n + \r)

Using \r (carriage return) allows overwriting the current line.

echo -ne "Loading: 10%\r"
sleep 1
echo -ne "Loading: 50%\r"
sleep 1
echo -ne "Loading: 100%\n"

Output Redirection

Write to File (Overwrite)

echo "Important Data" > file.txt

Append to File

echo "More Data" >> file.txt

Redirect Stderr

Technically echo writes to stdout, but you can redirect the entire block.

echo "Error: Something failed" >&2

Validating Variables

Check Variable Content

echo "Path is: $PATH"

Command Substitution

echo "Current date is: $(date)"

ANSI Color Output

You can use escape codes to print colored text. Requires -e.

Foreground Colors

Color Code
Black \033[30m
Red \033[31m
Green \033[32m
Yellow \033[33m
Blue \033[34m
Magenta \033[35m
Cyan \033[36m
White \033[37m
Reset \033[0m
echo -e "\033[31mError:\033[0m Something went wrong."
echo -e "\033[32mSuccess:\033[0m Operation completed."

Background Colors

Add 10 to the foreground code (e.g., Red Background is 41).

echo -e "\033[41mCritical Error\033[0m"

echo vs printf

echo behavior varies between shells (Bash, Zsh, Dash, Sh). For complex formatting or strict portability, printf is preferred.

Feature echo printf
Newline Automatic (unless -n) Manual (\n required)
Formatting Limited Advanced (%s, %d, %f)
Portability Inconsistent flags POSIX standard

Example where printf wins:

printf "User: %s, ID: %04d\n" "Alice" 5
# Output: User: Alice, ID: 0005


Practical Examples

Create a Config File

echo "server=127.0.0.1" > /etc/myapp.conf
echo "port=8080" >> /etc/myapp.conf

Empty a File (Truncate)

Quickly clear log files without deleting them.

echo -n > /var/log/syslog
# OR simply
> /var/log/syslog

Display Multi-line Text

echo "
Usage: script.sh [options]

Options:
  -h    Help
  -v    Verbose
"

Exit Status

Code Meaning
0 Success
1 Write error (e.g. disk full)

Notes

  • Quoting: Always quote strings containing special characters like *, ?, or spaces to avoid shell expansion.
    echo *   # Lists files in directory!
    echo "*" # Prints asterisk