echo Command in Linux Explained [With Examples]

Written by: Subhash Chandra   |   Last updated: September 25, 2023

The echo command in Linux is a built-in command-line tool that is used to print (or "echo") arguments it receives to the standard output, which by default is the terminal display. In addition to printing simple text messages, it can also be used to display the values of environment variables. It supports a number of options and escape sequences for special characters.

Syntax:

echo [OPTION]... [STRING]...
  • [OPTION] represents the optional flags that can modify the behavior of the command.
  • [STRING] is the text or variable you want to print to the terminal.

Options

The echo command options may vary slightly based on the shell or its implementation, but the following are the commonly supported options:

The following are the options typically available for the echo command in the bash shell:

  • -n: Do not output the trailing newline.
  • -e: Enable interpretation of backslash-escaped characters.
  • -E: Disable interpretation of backslash-escaped characters (default behavior).

Basic Usage

The echo command is primarily used to display text or output data to the console or terminal.

To print straightforward messages directly:

echo Hello, Linux user!
directly print message to terminal using echo command

This will display the text Hello, Linux user! on the terminal.

Variables can be defined and their values can be printed using the echo command. Example:

name="Linux"   ###Define variable
echo $name     ###Display the variable
print variable value to terminal using echo command

This will display the text "Linux" on the terminal.

Using echo Command

Escape Sequences

The echo command various escape sequences when used with the -e option. Escape sequences allow you to include special characters within the string you wish to echo. Here are the commonly used escape sequences:

  1. \a: Alert (bell). This produces an audible or visible alert, depending on the system configuration.
  2. \b: Backspace. Moves the cursor one position to the left.
  3. \c: Suppress further output. When this escape sequence is encountered, echo will not output anything else that follows in the string.
  4. \e: Escape character.
  5. \f: Form feed. On some systems, this might move the cursor to the next page or clear the screen.
  6. \n: Newline. Moves the cursor to the next line.
  7. \r: Carriage return. Returns the cursor to the beginning of the line without advancing to the next line.
  8. \t: Horizontal tab. Moves the cursor to the next tab stop.
  9. \v: Vertical tab.
  10. \\: Backslash. To display a literal backslash.
  11. \0NNN: Produces a byte with octal value NNN. NNN can be 0 to 3 octal digits.

To use these escape sequences effectively, you need to combine them with the -e option for echo. For instance:

echo -e "Hello \nLinux!"

This command will produce the output:

Hello
Linux!
echo -e "Hello, \nWorld! \nHow \nare \nyou."
Output:
Hello,
World!
How
are
you.

To add a horizontal tab before a word, use the \t escape character:

echo -e "Hello, \tWorld! \tHow \tare \tyou."
echo print with tab space

To add a vertical tab before a word, use the \v escape character:

echo -e "Hello, \vWorld! \vHow \vare \vyou."
echo print with vertical tab space

To change the color of the text, you can use the ANSI escape sequence:

echo -e ‘\033[1;37mHello’
echo -e ‘\033[0;30mWorld!’
echo -e ‘\033[0;31mHow’
echo -e ‘\033[0;32mare’
echo -e ‘\033[0;33myou.’

You can also use echo -e to use multiple escape sequences, such as \n for a new line and \t for a horizontal tab in a single command:

echo -e "Life is short.\n\t- A wise man."
combine multiple escape escape sequences with echo

Variable Expansion

Variable expansion (sometimes referred to as parameter expansion) in the shell refers to the process by which the shell replaces a variable (or parameter) with its value when it's referenced. Here the echo command, variable expansion allows you to display the value of a variable.

Example:

distro="Linux"
echo "Welcome, $distro!"

This will output:

Welcome, Linux!

Note: In situations where the variable name is adjacent to characters that could be misconstrued as part of the variable name, you can use braces ({}) to delimit the variable name.

To print the value of an environment variable simply reference the variable with a $ prefix. For example to print the value of the PATH environment variable:

echo $PATH
display environment variable value using echo command

There are also special variables like $? (exit status of the last command), $# (number of arguments passed to the script), $0 (name of the script itself), $1, $2,... (positional parameters). These can be expanded using the echo command just like regular variables.

For example to print the first four positional parameters:

echo ""\"$1\",\"$2\",\"$3\",\"$4\""

Note: If you enclose a variable in single quotes, it'll be treated as a literal string rather than being expanded to its value.

Redirecting Output

Like other commands, echo can redirect its output to other destinations such as files.

Examples:

echo "Hello, Linux!" > output.txt
redirect echo output to a file

Here we used the > operator, to redirect the output of echo to output.txt file. Remember this create a new file or overwrite an existing file with content.

Where this appends to the file:

echo "This is next line." >> output.txt
redirect echo output to a file by appending

You may also redirect both the stdout and stderr output using 2>&1.

One use case to create an empty file (overwrite file exists):

echo -n "" > file1.txt
create empty file using echo command

When you use the -n option, the echo command won't produce any output (not even a newline), so the redirection > will result in the creation of an empty file named file1.txt.

Command Substitution

Command substitution is a shell feature that allows you to execute a command and substitute its output in place of the command itself. We can use echo command, to display the result of a command execution.

Examples:

echo "Today's date is: $(date)"
echo display result of a command

This will print a message like "Today's date is: Mon 25 Sep 2023 15:55:06 AEST", depending on the current date and time.

Instead, you can also use backticks instead of $(). Example:

echo "Today's date is: `date`"
echo command using backtick for command substitution

Final Note: The echo command doesn't strictly require double quotes (" ") to function. However, using it can be beneficial or necessary in certain situations such as variable expansion, command expansion, handling special characters, and preventing globbing.

About The Author

Subhash Chandra

Subhash Chandra

Subhash Chandra, an Oracle Certified Database Administrator and professional writer, works as a Consulting User Assistance Developer at Oracle, bringing over 15 years of experience to the role. He enjoys sharing his technological passion through how-to articles, simplifying complex concepts in Linux, Windows, Mac OS, and various other platforms and technologies for a wide audience.

SHARE

Comments

Please add comments below to provide the author your ideas, appreciation and feedback.

Leave a Reply

Leave a Comment