echo Command

Purpose

Writes character strings to standard output.

Syntax

echo [ String ... ]

Description

The echo command writes character strings to standard output. The strings are separated by spaces, and a new-line character follows the last String parameter specified. If no String parameter is specified, a blank line (new-line character) is displayed.

Normally you might distinguish between a flag and a string that begins with a hyphen by using a -- (double hyphen). Since the echo command does not support any flags, a -- (double hyphen) is treated literally.

The echo command recognizes the following escape conventions:

Table 1. Escape conventions
Item Description
\a Displays an alert character.
\b Displays a backspace character.
\c Suppresses the new-line character that otherwise follows the final argument in the output. All characters that follow the \c sequence are ignored.
\f Displays a form-feed character.
\n Displays a new-line character.
\r Displays a carriage return character.
\t Displays a tab character.
\v Displays a vertical tab character.
\\ Displays a backslash character.
\0Number Displays an 8-bit character whose ASCII value is a 0, 1, 2, or 3-digit octal number.
Note: The bsh, ksh, and csh commands each contain a built-in echo subcommand. The echo command and the bsh and ksh echo subcommands work the same way. The csh echo subcommand does not work the same way as the echo command.

The \ (backslash) is a quotation mark character in the shell. This means that unless the \ is used with an escape character or enclosed in quotation marks, for example "\" or '\', the shell removes the backslashes when the command is expanded.

After shell expansion, the echo command writes the output based on the escape sequences in the input. Refer to the backslash reduction table for an example comparison of how backslashes in a command are reduced by the shell and then by the echo command:

Backslash reduction
Table 2. Backslash reduction
Command Entered After Shell Expansion After echo Command Processing
echo hi\\\\there echo hi\\there hi\there
echo 'hi\\\\there' echo 'hi\\\\there' hi\\there
echo "hi\\\\there' echo "hi\\there" hi\there

Exit status

This command returns the following exit values:

Table 3. Exit status
Item Description
0 Successful completion.
>0 An error occurred.

Examples

  1. To write a message to standard output, enter the following command:
    echo Please insert diskette . . .
  2. To display a message that contains special characters, enter the following command:
    echo "\n\n\nI'm at lunch.\nI'll be back at 1:00."
    This skips three lines and displays the message:
    I'm at lunch.
    I'll be back at 1:00.
    Note: You must put the message in quotation marks if it contains escape sequences. Otherwise, the shell interprets the \ (backslash) as a metacharacter and treats the \ differently.
  3. To use the echo command with pattern-matching characters, enter the following command:
    echo The back-up files are: *.bak

    This usage displays the message The backup files are: followed by the file names in the current directory that ends with .bak.

  4. To add a single line of text to a file, enter the following command:
    echo Remember to set the shell search path to $PATH. >>notes

    This usage adds the message to the end of the file notes after the shell substitutes the value of the PATH shell variable.

  5. To write a message to the standard error output, enter the following command:
    echo Error: file already exists. >&2

    This command redirects the error message to standard error. If the >&2 is omitted, the message is written to standard output.

Files

Table 4. Files
Item Description
/usr/bin/echo Contains the echo command.