The POSIX shell built-in echo writes its arguments to standard output and terminates. The following takes place before the output is generated:
As with every other command, the shell interprets the command line arguments and then passes the edited arguments to echo. The argument separators recognized by the shell are blanks and tabs.
echo interprets any remaining special characters that control output (see description of argument below).
echo displays processed arguments as follows:
Each argument is separated from the next by a blank even if you have entered more than one argument separator between individual arguments in the call.
A newline character is output at the end of the last argument.
You can use echo to
examine values of shell variables,
generate messages in shell scripts and thus test how they function,
test how the shell interprets a command call without the command being executed,
redirect data to a pipe and test how the pipe processes this input.
Besides the shell built-in echo, there is also a command called /usr/bin/echo. The shell generates a new process to execute /usr/bin/echo. Otherwise, /usr/bin/echo operates in the same way as echo.
Syntax
echo[ argument...] |
argument Any string delimited by blanks or tabs. The last argument is terminated by means of a command separator. You may specify any number of arguments, provided they are separated by at least one tab or blank character. As with any other command, this string is also initially interpreted by the shell:
The shell built-in echo also interprets the control characters described below. Since the backslash has a special meaning for the shell, it must be escaped:
Example
Control characters The following control characters influence the output of echo, provided the backslash has been appropriately escaped. Please note the terminal-specific characteristics.
echo prints the arguments entered up to this control character, omitting the newline and ignoring the remaining arguments.
(form feed) This control character is not converted if the output is written to the terminal.If the output from echo is sent to a printer, for example, all arguments entered after this control character will be written on the next page.
(newline) All arguments that follow the newline character are written in the next line.\n corresponds to enter key or CTRL+J
Control character for tab; echo moves the cursor to the next tab stop. Characters entered after \t are written by echo from this column onward. The setting of tab stops depends on the data terminal that is used. \t corresponds to TAB or CTRL+I
n must be a one, two or three figure octal number. The echo command outputs the corresponding character (see Tables and directories, EDF04 character set). In this way you can, for example, generate characters with an internal code greater than FF (EDF04) even if you do not possess an 8-bit terminal. A dummy character is output to represent non-displayable characters (device-dependent). If a character in octal notation is to be followed by an additional digit that is not part of the octal number, you must use the full three-digit form of the octal number. Example
If control characters of the above type are to be interpreted by neither the shell nor echo, they must be entered as follows: Example
The above example applies to the remaining strings as well. argument not specified: echo produces only a blank line. |
Locale
The following environment variables affect the execution of echo: LANG Provide a default value for the internationalization variables that are unset or null. If LANG is unset of null, the corresponding value from the implementation-specific default locale will be used. If any of the internationalization variables contains an invalid setting, the utility will behave as if none of the variables had been defined. LC_ALL If set to a non-empty string value, override the values of all the other internationalization variables. LC_CTYPE Determine the locale for the interpretation of sequences of bytes of text data as characters (for example, single- as opposed to multi-byte characters in arguments and input files), the classification of characters as upper- to lower-case, and the mapping of characters from one case to the other. LC_MESSAGES Determine the locale that should be used to affect the format and contents of diagnostic messages written to standard error. NLSPATH Determine the location of message catalogs for the processing of LC_MESSAGES. |
Example 1
The following interactive session will demonstrate when blanks, tabs and newline characters are retained:
|
Example 2
User anna wants to know what value is assigned to the HOME variable:
|
Example 3
The following line in a shell script will cause an error message to be written on standard error:
|
Example 4
A string constant is to be placed before the output of the date command:
The control character \c prevents echo from interpreting the newline. The backslash must be escaped despite the double quotes. |
See also
print, printf |