The Single UNIX ® Specification, Version 2
Copyright © 1997 The Open Group


read - read a line from standard input


read [-r] var...


The read utility will read a single line from standard input.

By default, unless the -r option is specified, backslash (\) acts as an escape character, as described in Escape Character (Backslash) . If standard input is a terminal device and the invoking shell is interactive, read will prompt for a continuation line when:

The line will be split into fields as in the shell (see Field Splitting ); the first field will be assigned to the first variable var, the second field to the second variable var, and so on. If there are fewer var operands specified than there are fields, the leftover fields and their intervening separators will be assigned to the last var. If there are fewer fields than vars, the remaining vars will be set to empty strings.

The setting of variables specified by the var operands will affect the current shell execution environment; see Shell Execution Environment . 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 supports the XBD specification, Utility Syntax Guidelines  .

The following option is supported:

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


The following operands are supported:
The name of an existing or non-existing shell variable.


The standard input must be a text file.




The following environment variables affect the execution of read:
Determine the internal field separators used to delimit fields. See Shell Variables .
Provide a default value for the internationalisation variables that are unset or null. If LANG is unset or null, the corresponding value from the implementation-dependent default locale will be used. If any of the internationalisation variables contains an invalid setting, the utility will behave as if none of the variables had been defined.
If set to a non-empty string value, override the values of all the other internationalisation variables.
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).
Determine the locale that should be used to affect the format and contents of diagnostic messages written to standard error.
Determine the location of message catalogues for the processing of LC_MESSAGES .
Provide the prompt string that an interactive shell will write to standard error when a line ending with a backslash is read and the -r option was not specified, or if a here-document is not terminated after a newline character is entered.




Not used.


Used for diagnostic messages and prompts for continued input.






The following exit values are returned:
Successful completion.
End-of-file was detected or an error occurred.




The read utility has historically been a shell built-in.

The -r option is included to enable read to subsume the purpose of the line utility, which has been marked LEGACY.

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.


The following command:

while read -r xx yy
	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.





UNIX ® is a registered Trademark of The Open Group.
Copyright © 1997 The Open Group
[ Main Index | XSH | XCU | XBD | XCURSES | XNS ]