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:
| 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. |
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:
| Item | Description |
|---|---|
| 0 | Successful completion. |
| >0 | An error occurred. |
Examples
- To write a message to standard output, enter the following command:
echo Please insert diskette . . . - 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. - To use the echo command with pattern-matching characters, enter the following
command:
echo The back-up files are: *.bakThis usage displays the message The backup files are: followed by the file names in the current directory that ends with .bak.
- To add a single line of text to a file, enter the following command:
echo Remember to set the shell search path to $PATH. >>notesThis usage adds the message to the end of the file notes after the shell substitutes the value of the PATH shell variable.
- To write a message to the standard error output, enter the following command:
echo Error: file already exists. >&2This command redirects the error message to standard error. If the
>&2is omitted, the message is written to standard output.
Files
| Item | Description |
|---|---|
| /usr/bin/echo | Contains the echo command. |