9.3.20 read command (reads from the standard input and stores the input in variables)
Syntax
read [-p] [-r] [-u [num]] [varname ...]
Description
This command reads one line at a time from the standard input and stores the input in specified shell variables.
Arguments
- -p
Specifies that the command is to read from the output of a background process through a pipe.
- -r
-
A \ (backslash) is not treated as an escape character.
If a \ (backslash) is included in the contents to be read, the read command treats the backlash as an escape letter. If a \ (backslash) is included in the line, the read command treats the backlash as an escape letter even if the character followed by the backlash is the delimiter defined by using the shell variable IFS. If a backlash is specified at the end of the line, the next line is continuously read. Use the -r option if the \ (backslash) is not to be treated as an escape character.
- -u [num]
Specifies the file identifier of the file from which the input is to be read. If no value is specified, the standard input is read.
Specify either a file identifier or p. Specifying p is equivalent to specifying the -p option.
- varname
Specifies the name of a variable in which input that is read is to be stored.
When multiple variable names are specified, the input line is split into the fields delimited by the delimiter set in the IFS variable, and the fields are assigned to the variable names in sequential order (input line's first field is set in the first variable, second field is set in the second variable, and so on).
If the number of fields exceeds the number of variable names, the values of all the remaining fields are stored in the last variable that is specified.
If there are fewer fields than there are variables, a linefeed is set in each additional variable.
Return codes
Return code |
Meaning |
---|---|
0 |
Normal termination |
1 |
Normal termination
Error termination
|
Notes
Even if the command syntax of this regular built-in command is invalid, it does not exit the shell that is executing the command.
When executing the read command in a separate process, such as the & operator and a command substitution, also see the notes provided in 5.1.7 Execution as a separate process.
In Windows, there are circumstances where keyboard input is accepted even before the read command starts. For example, this can happen while the system is executing a previous command, or when you are reading input from the console or command prompt while running the debugger within an editor and the debugger is stopped at a breakpoint. To prevent the input from being read incorrectly, do not make entries from the keyboard before the read command starts.
If there is input from the keyboard before the read command starts, that input will be displayed when the read command starts. At that point, delete the keyboard input that is displayed and re-enter it for the read command.
-
If the character string read from standard input uses [LF] as the linefeed code, the read command stores the string in the variable with the [LF] characters removed. If the linefeed code is [CR]+[LF], the read command stores the string with the [CR] and [LF] characters removed.
Examples
Read the file string.txt and send its contents to the standard output.
Contents of the job definition script
while read LINE do echo "$LINE" done < string.txt
Contents of the STDOUT file of the execution job
******** JOB SCOPE STDOUT ******** HITACHI JP1 Advanced Shell
Read a character string output to the standard output by the coproc.sh background process and store it in the variable NAME.
coproc.sh |& read -p NAME