echo Command

Purpose

Writes character strings to standard output.

Syntax

echo [ String ... ]

Description

The echo command writes character strings to standard output. 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 could distinguish between a flag and a string that begins with a hyphen by using a — (double hyphen). Since no flags are supported with the echo command, a — (double hyphen) is treated literally.

The echo command recognizes the following 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 following 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. For information on the echo subcommands, see "Bourne shell built-in commands," "Regular built-in command descriptions for the Korn shell or POSIX shell," and "C shell built-in commands" in AIX® Version 7.1 Operating system and device management.

The \ (backslash) is a quote character in the shell. This means that unless the \ is used with an escape character or enclosed in quotes, 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 first reduced by the shell and then by the echo command:

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:

Item Description
0 Successful completion.
>0 An error occurred.

Examples

  1. To write a message to standard output, enter:
    echo Please insert diskette . . .
  2. To display a message containing special characters, enter:
    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:
    echo The back-up files are: *.bak
    This usage displays the message The back-up files are: followed by the file names in the current directory ending with .bak.
  4. To add a single line of text to a file, enter:
    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:
    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.

File

Item Description
/usr/bin/echo Contains the echo command.