read command (reads from the standard input and stores the input in variables)
- Organization of this page
Format
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
-
Specifies that when there is a \ at the end of a line, the next line is not to be read as a continuation line.
- -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 in a separate process (UNIX only).
-
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.
Usage 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