Your Browser is not longer supported

Please use Google Chrome, Mozilla Firefox or Microsoft Edge to view the page correctly
Loading...

{{viewport.spaceProperty.prod}}

echo - write arguments to standard output

&pagelevel(4)&pagelevel

The POSIX shell built-in echo writes its arguments to standard output and terminates. The following takes place before the output is generated:

  1. 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.

  2. echo interprets any remaining special characters that control output (see description of argument below).

  3. 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:

  • If the string contains an asterisk, question mark, or [...], the shell replaces this string by all suitable file names in the current directory. If no suitable file name is found, the shell passes the string on to echo unaltered.

  • 'string'
    All shell metacharacters are escaped by the single quotes. The shell passes the string as an argument to echo without the quotes. All blanks, tabs and newline characters entered within single quotes are retained.

  • `string`
    The shell executes string as a POSIX command and passes the output of this command to echo. Characters assigned to the environment variable IFS are interpreted by the shell in the output as argument delimiters. The default characters assigned to IFS are the blank, tab or newline characters.
  • "string"
    The double quotes escape all shell metacharacters except for the backslash \, backquotes `...` and the dollar sign $. The shell passes the processed string to echo as an argument. All blanks, tabs, and newline characters within the double quotes are retained.

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:

  • by preceding it with another backslash \
    This also applies if the argument containing this control character is enclosed within double quotes.

  • by single quotes '...'
    If the argument containing this control character is enclosed within single quotes, the backslash need not be escaped again.


Example

$ echo hello\\tuser
hello   user
$ echo hello'\'tuser
hello   user
$ echo 'hello\tuser'
hello   user
$ echo "hello\tuser"
hello   user


Control characters

The following control characters influence the output of echo, provided the backslash has been appropriately escaped.

Please note the terminal-specific characteristics.

\c

echo prints the arguments entered up to this control character, omitting the newline and ignoring the remaining arguments.

\f

(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.
\f corresponds to CTRL+L

\n

(newline)

All arguments that follow the newline character are written in the next line.\n corresponds to enter key or CTRL+J

\t

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

\0n

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


$ echo ’\0337’|od -xc
0000000    df15
         337  \n
0000002
$ echo ’\00337’|od -xc
0000000    1bf7    1500
         033   7  \n
0000003


If control characters of the above type are to be interpreted by neither the shell nor echo, they must be entered as follows:

Example


$ echo \\
\
$ echo \\\\t
\t
$ echo ’\\t’
\t
$ echo "\\\t"
\t


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:


$ echo Today is      Monday.
Today is Monday.

In this case echo receives three arguments and returns them with one blank between each.


$ echo "Today is     Monday."
Today is     Monday.

All blanks entered within the double quotes are retained.


Example 2

User anna wants to know what value is assigned to the HOME variable:

$ echo $HOME
/home/anna

Example 3

The following line in a shell script will cause an error message to be written on standard error:

$ echo File $1 not found >&2

Example 4

A string constant is to be placed before the output of the date command:

$ echo "Today's date and time:  \\c"; date
Today's date and time:  Thu Jun 25 19:58:09 MSZ 2020

The control character \c prevents echo from interpreting the newline. The backslash must be escaped despite the double quotes.

See also

print, printf