Echo Command in Linux with Examples

By 

Updated on

5 min read

Echo command color output in a Linux terminal

The echo command is one of the most basic and frequently used commands in Linux. It prints its arguments to the standard output.

echo is commonly used in shell scripts to display messages or output the results of other commands. This guide covers the Bash builtin version of echo with practical examples.

Syntax

Terminal
echo [OPTIONS] [ARGUMENTS]

echo is a shell builtin in Bash and most other popular shells like Zsh and Ksh. There is also a standalone /usr/bin/echo utility, but the shell builtin version takes precedence.

The available options are:

  • -n — Do not output a trailing newline.
  • -e — Enable interpretation of backslash escape sequences.
  • -E — Disable interpretation of escape sequences (this is the default).

When -e is used, the following escape sequences are recognized:

  • \\ — Backslash.
  • \a — Alert (BEL).
  • \b — Backspace.
  • \c — Suppress further output.
  • \e — Escape character.
  • \f — Form feed.
  • \n — New line.
  • \r — Carriage return.
  • \t — Horizontal tab.
  • \v — Vertical tab.

Display a String

To display a simple line of text, pass it as an argument to echo:

Terminal
echo Hello, World!
output
Hello, World!

Although not required, it is a good practice to enclose arguments in double or single quotes. When using single quotes '', the literal value of each character is preserved and variables are not expanded.

Display Strings with Quotes

To print a double quote, enclose the text within single quotes or escape it with a backslash:

Terminal
echo 'Hello "Linuxize"'
echo "Hello \"Linuxize\""
output
Hello "Linuxize"

To print a single quote, enclose the text within double quotes:

Terminal
echo "I'm a Linux user."
output
I'm a Linux user.

Use Escape Characters

Use the -e option to enable interpretation of escape sequences. In the following example, we are using \n for a new line and \t for a horizontal tab:

Terminal
echo -e "You know nothing, Jon Snow.\n\t- Ygritte"
output
You know nothing, Jon Snow.
	- Ygritte

Suppress the Trailing Newline

By default, echo appends a newline character at the end of the output. Use the -n option to suppress it:

Terminal
echo -n "Hello, " && echo "World!"
output
Hello, World!

This is useful when building prompts or combining multiple outputs on a single line inside a script.

Display Variables

echo can display shell variables . In the following example, we are printing the name of the currently logged-in user:

Terminal
echo "Current user: $USER"
output
Current user: linuxize

Display Command Output

Use the $(command) expression to include command output in the echo argument. The following command displays the current date :

Terminal
echo "The date is: $(date +%D)"
output
The date is: 04/17/19

Use Pattern Matching

The shell expands wildcard characters before passing arguments to echo. For example, the following command returns the names of all .php files in the current directory:

Terminal
echo The PHP files are: *.php
output
The PHP files are: index.php contact.php functions.php

Redirect Output to a File

Instead of displaying the output on the screen, you can redirect it to a file using the > or >> operators:

Terminal
echo "First line" > /tmp/file.txt
echo "Second line" >> /tmp/file.txt

When using >, the file is overwritten. The >> operator appends the output to the file . If the file does not exist, both operators create it.

Use the cat command to verify the contents:

Terminal
cat /tmp/file.txt
output
First line
Second line

Write to Standard Error

To send output to standard error instead of standard output, redirect file descriptor 1 to 2:

Terminal
echo "Error: something went wrong" >&2

This is useful in scripts where you need to separate error messages from normal output. For more details, see how to redirect stderr to stdout in Bash .

Display Colored Output

Use ANSI escape sequences to change the foreground and background colors or set text properties such as bold and underline:

Terminal
echo -e "\033[1;37mWHITE"
echo -e "\033[0;30mBLACK"
echo -e "\033[0;34mBLUE"
echo -e "\033[0;32mGREEN"
echo -e "\033[0;36mCYAN"
echo -e "\033[0;31mRED"
echo -e "\033[0;35mPURPLE"
echo -e "\033[0;33mYELLOW"
echo -e "\033[1;30mGRAY"
Echo command color output in a Linux terminal

Quick Reference

TaskCommand
Print a stringecho "text"
Print without trailing newlineecho -n "text"
Print with escape sequencesecho -e "line1\nline2"
Print a variableecho "$VAR"
Print command outputecho "$(command)"
Write to a file (overwrite)echo "text" > file.txt
Append to a fileecho "text" >> file.txt
Write to stderrecho "error" >&2

FAQ

What is the difference between echo and printf?
echo automatically adds a trailing newline and has limited formatting options. printf supports format specifiers (like %s, %d) and does not add a newline unless you include \n. Use printf when you need precise control over output formatting.

How do I print a newline with echo?
Use the -e option with the \n escape sequence: echo -e "line1\nline2". Without -e, the backslash sequence is printed literally.

How do I echo without a newline?
Use the -n option: echo -n "text". This suppresses the trailing newline character.

Why does echo behave differently on macOS?
macOS ships with a BSD version of echo that does not support the -e option by default. Use /usr/bin/echo or switch to printf for consistent behavior across platforms.

Conclusion

The echo command prints text to the terminal and is one of the most commonly used commands in Bash scripts. Use -n to suppress the trailing newline and -e to interpret escape sequences. For more advanced formatting, consider using printf .

If you have any questions, feel free to leave a comment below.

Tags

Linuxize Weekly Newsletter

A quick weekly roundup of new tutorials, news, and tips.

About the authors

Dejan Panovski

Dejan Panovski

Dejan Panovski is the founder of Linuxize, an RHCSA-certified Linux system administrator and DevOps engineer based in Skopje, Macedonia. Author of 800+ Linux tutorials with 20+ years of experience turning complex Linux tasks into clear, reliable guides.

View author page