Linux echo command

Updated: 11/30/2020 by Computer Hope
echo command

On Unix-like operating systems, the echo command prints text to standard output, e.g., the terminal.

This page covers the GNU/Linux version of echo.

Description

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

Most command shells, including bash, ksh and csh implement echo as a built-in command. The behavior of built-in echo commands is similar, but the options may be different; those commands are not documented here.

This page covers the stand-alone program, /bin/echo. Its options are slightly different than the built-in echo command that is included in your shell. If you are using the bash shell, you can determine which echo is the default, using the type command:

type echo
echo is a shell builtin

To specify that you want to run the stand-alone program instead of the shell built-in, use its complete path in the command, i.e., run it like this:

/bin/echo

This page describes the GNU/Linux stand-alone version of echo.

Syntax

echo [SHORT-OPTION]... [STRING]...
echo --help
echo --version

Options

Options

These options may be specified before the string, and affect the behavior of echo.

-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.

Options

If a long option is specified, you may not specify a string to be echoed. These options are for getting information about the program only.

--help Display a help message and exit.
--version Output version information and exit.

Escape sequences

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

Sequence Interpreted as
\\ 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 Esc.
\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

/bin/echo Hello, world!

In the above command, the two words (Hello, and world!) are passed to echo as separate arguments, and echo prints them in sequence, separated by a space:

Hello, world!

The next command produces the same output:

/bin/echo 'Hello, World!'
Hello, world!

However, unlike the first example, the above command provides the single-quoted string 'Hello, world!' as a single argument.

Single-quoting a string will reliably protect it from interpretation by the shell, passing special characters and escape sequences literally to echo.

For instance, in the bash shell, variable names are preceded by a dollar sign ($). In the next command, the variable name inside the quotes is treated literally; outside the quotes, it is converted to its value.

/bin/echo 'The value of $PATH is' $PATH
The value of $PATH is /home/hope/bin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

Escape sequences are not interpreted, by default:

/bin/echo 'Here, \bthe \bbackspace \bsequences \bare \bignored.'
Here, \bthe \bbackspace \bsequences \bare \bignored.

However, if you provide the -e option, they are interpreted:

/bin/echo -e 'Here, \bhowever, \bthe \bbackspace \bsequences \bare \binterpreted.'
Here,however,thebackspacesequencesareinterpreted.

If you need to insert newlines in your echo output, specify the -e option and include the \n escape sequence wherever you want a new line:

echo -e 'Here,\nwe\nhave\ninserted\nnewlines.'
Here,
we
have
inserted
newlines.

Same for tabs:

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

Another example:

echo -e 'This line is not completely \cprinted.'
This line is not completely

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.