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. |
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 ReductionCommand 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:
echo Please insert diskette . . .
- To display a message
containing special characters, enter:
This skips three lines and displays the message:echo "\n\n\nI'm at lunch.\nI'll be back at 1:00."
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:
This usage displays the message The back-up files are: followed by the file names in the current directory ending with .bak.echo The back-up files are: *.bak
- To add a single line
of text to a file, enter:
This usage adds the message to the end of the file notes after the shell substitutes the value of the PATH shell variable.echo Remember to set the shell search path to $PATH. >>notes
- To write a message to
the standard error output, enter:
This command redirects the error message to standard error. If the >&2 is omitted, the message is written to standard output.echo Error: file already exists. >&2
File
Item | Description |
---|---|
/usr/bin/echo | Contains the echo command. |