Linux echo command

Updated: 12/29/2017 by Computer Hope

About echo

The echo program displays text. It's a handy way to create customized output in your terminal.

Note: This document covers the standalone program, /bin/echo. Its options are slightly different than the builtin 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 standalone program instead of the shell builtin, use its complete path in the command, i.e., run it like this:

/bin/echo

Overview

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 builtin command. The behavior of builtin echo commands is similar, but the options may be different; those commands are not documented here.

Syntax

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

Options

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

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

echo 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 will be 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.