echo

utilitieslinux
The echo command is one of the most frequently used commands in Linux/Unix-like operating systems. echo The echo command displays text or variables to standard output. It is one of the simplest yet most frequently used commands in shell scripting, allowing users to print messages, variable values, or command outputs to the terminal or redirect them to files.

Quick Reference

Command Name:

echo

Category:

utilities

Platform:

linux

Basic Usage:

echo [options] [arguments]

Common Use Cases

  • 1

    Text output

    Display text on the console or write to files

  • 2

    Scripting

    Use in shell scripts to output text or variables

  • 3

    Debugging

    Print variables or debug information

  • 4

    Automation

    Automate text output in scripts

Syntax

echo [OPTION]... [STRING]...

Options

Option Description
-n Do not output the trailing newline
-e Enable interpretation of backslash escapes
-E Disable interpretation of backslash escapes (default)
--help Display help information
--version Display version information

Common Backslash Escapes (with -e option):

Sequence Description
\n New line
\t Horizontal tab
\v Vertical tab
\b Backspace
\r Carriage return
\f Form feed
\\ Backslash
\a Alert (bell)
\0NNN Character with octal value NNN
\xHH Character with hexadecimal value HH

Examples

How to Use These Examples

The examples below show common ways to use the echo command. Try them in your terminal to see the results. You can copy any example by clicking on the code block.

#

Basic Examples:

echo "Hello, World!"

Display a simple text message.

echo Hello World

Echo works without quotes for simple messages (without special characters).

echo -n "No newline"

Print text without adding a trailing newline character.

echo "Current user: $USER"

Display text with variable expansion.

Advanced Examples:

echo -e "Line 1\nLine 2"

Use escape sequences to format text (enables interpretation of backslash escapes).

echo "Hello" > greeting.txt

Redirect output to a file (overwrites existing content).

echo "World" >> greeting.txt

Append output to an existing file.

echo -e "\033[31mRed text\033[0m"

Use ANSI escape codes to display colored text (in supported terminals).

echo `date`

Display the output of a command (using command substitution).

echo $(( 5 + 7 ))

Perform arithmetic operations and display the result.

Try It Yourself

Practice makes perfect! The best way to learn is by trying these examples on your own system with real files.

Understanding Syntax

Pay attention to the syntax coloring: commands, options, and file paths are highlighted differently.

Notes

Key Points:

  • The echo command is built into most shells, including Bash, but also exists as a standalone command
  • It's one of the most basic and frequently used commands for displaying text or values
  • Behavior may vary slightly between different shell implementations
  • Echo is heavily used in shell scripts for displaying messages and debugging
  • When using variables, double quotes preserve spaces but allow variable expansion, while single quotes preserve everything literally

Shell Variations:

  • Bash echo: Supports -n, -e and -E options by default
  • POSIX echo: May have different behavior regarding escape sequences
  • /bin/echo: The standalone binary may behave differently than the shell builtin
  • zsh/ksh/dash: Minor variations in option handling and escape sequence interpretation
  • To ensure consistent behavior across systems, consider using printf for complex formatting

Common Use Cases:

  • Displaying status messages in shell scripts
  • Creating or appending content to files
  • Debugging scripts by displaying variable values
  • Generating simple text files or configuration files
  • Sending data to other commands through pipes
  • Checking environment variables or script execution flow

Related Commands:

  • printf - More advanced formatted output with more consistent behavior across systems
  • cat - Display file contents or create files from standard input
  • tee - Read from standard input and write to both standard output and files
  • yes - Output a string repeatedly until killed
  • write - Send a message to another user's terminal

Tips for Effective Use:

  • For scripts intended to run on multiple systems, use simple echo commands or prefer printf
  • Use double quotes around variables to preserve spaces but allow expansion
  • The -e option is needed to interpret special characters like \n (newline) and \t (tab)
  • Combine with command substitution $(command) or `command` to echo command outputs
  • Use >> to append to files instead of > to avoid overwriting existing content

Tips & Tricks

1

Use the -n option to suppress trailing newline

2

Use the -e option to enable backslash escapes

3

Use the -E option to disable backslash escapes

4

Use the -E option to enable interpretation of backslash escapes

5

Use the -E option to enable interpretation of backslash escapes

Common Use Cases

Text output

Display text on the console or write to files

Scripting

Use in shell scripts to output text or variables

Debugging

Print variables or debug information

Automation

Automate text output in scripts

Data processing

Manipulate and transform text data

Related Commands

These commands are frequently used alongside echo or serve similar purposes:

Use Cases

1

Text output

Display text on the console or write to files

2

Scripting

Use in shell scripts to output text or variables

3

Debugging

Print variables or debug information

4

Automation

Automate text output in scripts

5

Data processing

Manipulate and transform text data

Learn By Doing

The best way to learn Linux commands is by practicing. Try out these examples in your terminal to build muscle memory and understand how the echo command works in different scenarios.

$ echo
View All Commands