Reads one line from standard input.
read [ -p ][ -r ][ -s ][ -u[ n ] ] [ VariableName?Prompt ]
[ VariableName ... ]
The read command reads one line from standard input and assigns the values of each field in the input line to a shell variable using the characters in the IFS (Internal Field Separator) variable as separators. The VariableName parameter specifies the name of a shell variable that takes the value of one field from the line of input. The first shell variable specified by the VariableName parameter is assigned the value of the first field, the second shell variable specified by the VariableName parameter is assigned the value of the second field, and so on, until the last field is reached. If the line of standard input has more fields than there are corresponding shell variables specified by the VariableName parameter, the last shell variable specified is given the value of all the remaining fields. If there are fewer fields than shell variables, the remaining shell variables are set to empty strings.
Note: If you omit the VariableName parameter, the variable REPLY is used as the default variable name.
The setting of shell variables by the read command affects the current shell execution environment.
-p | Reads input from the output of a process run by the Korn Shell using |& (pipe, ampersand).
Note: An end-of-file character with the -p flag causes cleanup for this process to so that another can be spawned. |
-r | Specifies that the read command treat a \ (backslash) character as part of the input line, not as a control character. |
-s | Saves the input as a command in the Korn Shell history file. |
-u [ n ] | Reads input from the one-digit file descriptor number, n. The file descriptor can be opened with the ksh exec built-in command. The default value of the n is 0, which refers to the keyboard. A value of 2 refers to standard error. |
This command returns the following exit values:
0 | Successful completion. |
>0 | Detected end-of-file character or an error occurred. |
while read -r xx yy do print printf "%s %s/n" $yy $xx done < InputFile
read word1?"Please enter: " word2The system displays:
Please enter: You enter: hello worldThe value of the word1 variable should have "hello" and word2 should have "world."
(read; print "hello $REPLY") print -p "world" read-p lineThe value of the line variable should have "hello world."
read -s line < input_fileIf input_file contains "echo hello world," then "echo hello world" will be saved as a command in the history file.
The printf command.
The ksh command.