NAME
read — read a line
from standard input
SYNOPSIS
read |
[-r] var
... |
DESCRIPTION
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.
OPTIONS
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:
-r-
Do not treat a backslash character in any special way. Consider each backslash to be part of the input line.
OPERANDS
The following operands shall be supported by the implementation:
- var
-
The name of an existing or nonexisting shell variable.
STANDARD INPUT
The standard input shall be a text file.
INPUT FILES
None.
ENVIRONMENT VARIABLES
The following environment variables shall affect the execution of
read:
IFS-
This variable shall determine the internal field separators used to delimit fields. See 3.5.3.
LANG-
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.
LC_ALL-
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.
LC_CTYPE-
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).
LC_MESSAGES-
This variable shall determine the language in which messages should be written.
ASYNCHRONOUS EVENTS
Default.
STANDARD OUTPUT
None.
STANDARD ERROR
Used only for diagnostic messages.
OUTPUT FILES
None.
EXTENDED DESCRIPTION
None.
EXIT STATUS
The read utility shall exit with one of
the following values:
CONSEQUENCES OF ERRORS
Default.
RATIONALE
EXAMPLES
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.
HISTORY OF DECISIONS MADE
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.