read — Read a line from standard input

Format

read [–prs] [–u[d]] [variable?prompt] [variable …]

Description

When you call read without options, it reads one line from the standard input, breaks the line into fields, and assigns the fields to each variable in order.

To determine where to break the line into fields, read uses the built-in variable IFS (which stands for internal field separator). Encountering any of the characters in IFS means the end of one field and the beginning of the next. The default value of IFS is blank, tab, and newline.

In general, a single IFS character marks the end of one field and the beginning of the next. For example, if IFS is colon (:), read considers the input a::b to have three fields: a, an empty field, and b. However, if IFS contains blanks, tabs or escaped newlines, read considers a sequence of multiple blanks, tabs, or escaped newlines to be a single field separator. For example, a   b has two fields, even though there are several blanks between the a and b.

The nth variable in the command line is assigned the nth field. If there are more input fields than there are variables, the last variable is assigned all the unassigned fields. If there are more variables than fields, the extra variables are assigned the null string ("").

Start of changeBy default, unless the read option is specified, <backslash> acts as an escape character. An unescaped <backslash> preserves the literal value of the following character, with the exception of a <newline>. If a <newline> follows the <backslash>, the read utility interprets this as line continuation. The <backslash> and <newline> are removed before splitting the input into fields. All other unescaped <backslash> characters are removed after the input is split into fields. End of change

The environment variable REPLY is assigned the input when no variables are given. The exit status of read is 0, unless it encounters the end of the file.

Options

–p
Receives input from a coprocess.
–r
Treats input as raw data, ignoring escape conventions. For example, read –r does not interpret a final backslash (\) as a line continuation character, but as part of the input.
–s
Adds input to the command history file as well as to the variables specified with variable.
–u[d]
Reads input from the single-digit file descriptor d, rather than from the standard input. The default file descriptor is 0.
When the first variable parameter has the form:
variable?prompt
it defines a prompt for input. If the shell is interactive, read sends the prompt to the file descriptor d if it is open for write and is a terminal device. The default file descriptor for the prompt is 2.

Examples

IFS=':'
while read name junk junk1 junk2 junk3
do
  echo $name
done </samples/comics.lst
provides a list of comic names from the sample comics.lst file.

Environment variables

read uses the following environment variables:
IFS
Contains a string of characters to be used as internal field separators.
PS2
Contains the prompt string that an interactive shell uses when it reads a line that ends with a backslash and you did not specify the –r option, or if a here-document is not terminated after you enter a newline.
REPLY
Contains the input (including separators) if you did not specify any variables. The ability of omitting the variable from the command and using the environment variable REPLY is an extension.

Localization

read uses the following localization environment variables:
  • LANG
  • LC_ALL
  • LC_MESSAGES
  • NLSPATH

See Localization for more information.

Usage notes

read is a built-in shell command.

Exit values

0
Successful completion
1
Failure due to any of the following reasons:
  • End of file on input
  • Incorrect variable
  • Incorrect descriptor specified after –u
  • Missing coprocess
2
Incorrect command-line argument

Messages

Possible error messages include:
Cannot read on file descriptor …
You tried to read a file descriptor that was not opened for reading.

Portability

POSIX.2, X/Open Portability Guide.

The –p, –s, and –u options are extensions of the POSIX standard.

Related information

continue, fc, print, sh