Linux and Unix echo command

Quick links

About echo
Syntax
Examples
Related commands
Linux and Unix main page

About echo

echo displays a line of text.

Overview

echo is a fundamental command found in most operating systems that offer a command line. It is frequently used in scripts, batch files, and as part of individual commands; anywhere you may need to insert text.

Many command shells such as bash, ksh and csh implement echo as a built-in command.

bash is the default command shell in nearly every major Linux distribution, so in this documentation we will look at the behavior, syntax, and options of bash's implementation of echo.

Syntax

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

Options

-n Do not output a trailing newline.
-e Enable interpretation of backslash escape sequences (see below for a list of these).
-E Disable interpretation of backslash escape sequences (this is the default).
--help Display a help message and exit.
--version Output version information and exit.

If you specify the -e option, the following escape sequences are recognized:

\\ A literal backslash character ("\").
\a An alert (The BELL character).
\b Backspace.
\c Produce no further output after this.
\e The escape character; equivalent to pressing the escape key.
\f A form feed.
\n A newline.
\r A carriage return.
\t A horizontal tab.
\v A vertical tab.
\0NNN byte with octal value NNN (which can be 1 to 3 digits).
\xHH byte with hexadecimal value HH (which can be either 1 or 2 digits)

NOTE: each shell generally has its own implementation of echo, which may be slightly different than the version described here. Refer to your shell's documentation for details about the options it supports.

Examples

echo Hello, World!

Outputs the following text:

Hello, world!
x=10
echo The value of x is $x.

Entering these two commands will output the following text:

The value of x is 10.
echo -e 'Here \bthe \bspaces \bare \bbackspaced.'

Outputs the following text:

Herethespacesarebackspaced.

(Note that in order for the escape sequence to be protected from the shell, we have enclosed the echo string in quotes.)

echo -e 'Here\nwe\nhave\ninserted\nnewlines.'

Outputs the following text:

Here
we
have
inserted
newlines.
echo -e 'Here\twe\thave\tinserted\thorizontal\ttabs.'

Outputs the following text:

Here     we      have    inserted        horizontal      tabs.
echo -e 'This line is not completely \cprinted.'

Outputs the following text:

This line is not completely 
echo 'This text is now in a text file.' > textfile.txt

Writes the text This text is now in a text file. to the file textfile.txt. If textfile.txt does not exist, it will be created; if textfile.txt already exists, it will be overwritten.

echo 'This text is now in a text file.' >> textfile.txt

Appends the text This text is now in a text file. to the file textfile.txt. If textfile.txt does not exist, it will be created.

echo *

Output a string comprising the name of each file in the working directory, with each name separated by a space. For example, if you have three files in your working directory, output may resemble the following:

flower.jpg document.doc readme.txt
echo * | wc -w

This command will output the three filenames, just as above, but instead of printing them to the terminal, it will pipe the output to the wc command, which will display a count of the number of words in the output; this is a quick and simple way to find out how many files are in the working directory. So if we have three files, the output of this command will be:

3

For more on this particular topic, see "How can I see how many files or directories are in a Linux directory?"

Related commands

cat — Output the contents of a file.
printf — Write formatted output.
tac — Output the contents of files in reverse order.
tee — Route a file's contents to multiple outputs.
touch — Update the timestamp of a file or directory.
tr — Translate one set of characters to another.