Introduction
The
echo
command is a built-in feature in
Linux
that prints out its arguments as standard output. It is used to display text strings or the command results.
This tutorial explains different ways to use the
echo
command in Linux through various examples.
The
echo
command has several arguments. The following table presents commonly used
echo
command options.
Option | Description |
---|---|
-n
|
Displays the output while omitting the newline after it. |
-E
|
The default option. Disables the interpretation of escape characters. |
-e
|
Enables the interpretation of escape characters. |
--help
|
Displays a help message with information about the echo command and its options. |
--version
|
Prints the echo command version information. |
echo Command Examples
The
echo
command prints text or variables in the terminal. It's commonly used in
scripts
and
command-line
operations to provide feedback, print messages, or output variable values. The following text presents ways to use the
echo
command in Linux.
Printing a String
Run the following command to print
Hello, World!
as the output:
echo Hello, World!
The
-e
option is used with escape characters, as it enables their use in the output. The escape characters are useful for formatting output and adding special characters or effects to text displayed by the
echo
command. Escape characters used with the
-e
option are presented in the table below.
Escape Character | Description |
---|---|
\\
|
Displays a backslash character. |
\a
|
Plays a sound alert when displaying the output. |
\b
|
Removes all the spaces between the text. |
\c
|
Omits any output following the escape character. |
\n
|
Adds a newline character to the output, which signifies the end of one line of text and the beginning of a new line. |
\r
|
Performs a carriage return, which moves the cursor to the beginning of the current line without advancing to the next line. |
\t
|
Creates horizontal tab spaces. |
\v
|
Creates vertical tab spaces. |
For instance, using
\c
lets you shorten the output by omitting the part of the string that follows the escape character:
echo -e 'Hello, World! \c This is PNAP!'
For more Linux commands, check out our Linux Command Cheat Sheet .