READ(1) General Commands Manual READ(1)

readread a line from standard input

read [-r] var ...

The read utility shall read a single line from standard input. By default, unless the −r option is specified, backslash (\) shall act as an escape character, as described in 3.2.1. The line shall be split into fields (see the definition in 3.1.3) as in the shell (see 3.6.5); the first field shall be assigned to the first variable var, the second field to the second variable var, etc. If there are fewer var operands specified than there are fields, the leftover fields and their intervening separators shall be assigned to the last var. If there are fewer fields than vars, the remaining vars shall be set to empty strings. The setting of variables specified by the var operands shall affect the current shell execution environment; see 3.12.

The read utility shall conform to the utility argument syntax guidelines described in 2.10.2. The following option shall be supported by the implementation:

Do not treat a backslash character in any special way. Consider each backslash to be part of the input line.

The following operands shall be supported by the implementation:

var

The name of an existing or nonexisting shell variable.

The standard input shall be a text file.

None.

The following environment variables shall affect the execution of read:

This variable shall determine the internal field separators used to delimit fields. See 3.5.3.

This variable shall determine the locale to use for the locale categories when both LC_ALL and the corresponding environment variable (beginning with LC_ ) do not specify a locale. See 2.6.

This variable shall determine the locale to be used to override any values for locale categories specified by the settings of LANG or any environment variables beginning with LC_. 2.6.

This variable shall determine the locale for the interpretation of sequences of bytes of text data as characters (e.g., single- versus multibyte characters in arguments).

This variable shall determine the language in which messages should be written.

Default.

None.

Used only for diagnostic messages.

None.

None.

The read utility shall exit with one of the following values:

Successful completion.

End-of-file was detected or an error occurred.

Default.

The following command: while read -r xx yy do printf "%s %s\n" "$yy" "$xx" done < input_file prints a file with the first field of each line moved to the end of the line. The text in 2.11.5.2 indicates that the results are undefined if an end-of-file is detected following a backslash at the end of a line when −r is not specified. Since read affects the current shell execution environment, it is generally provided as a shell regular built-in. If it is called in a subshell or separate utility execution environment, such as one of the following:

( read foo) nohup read ... find . -exec read ... \;

it will not affect the shell variables in the caller’s environment.

The read utility has historically been a shell built-in. It was separated off into its own clause to take advantage of the standard’s richer description of functionality at the utility level. The −r option was added to enable read to subsume the purpose of the historical line utility.

September 1991 posix.fail