read Command
Purpose
Reads one line from standard input.
Syntax
read [ -p ][ -r ][ -s ][ -u[ n ] ] [ VariableName?Prompt ]
[ VariableName ... ]
Description
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.
The setting of shell variables by the read command affects the current shell execution environment.
Flags
Item | Description |
---|---|
-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
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. |
Parameters
Item | Description |
---|---|
VariableName?Prompt | specifies the name of one variable, and a prompt to be used. When the Korn Shell is interactive, it will write the prompt to standard error, and then perform the input. If Prompt contains more than one word, you must enclose it in single or double quotes. |
VariableName... | specfies one or more variable names separated by white space. |
Exit Status
This command returns the following exit values:
Item | Description |
---|---|
0 | Successful completion. |
>0 | Detected end-of-file character or an error occurred. |
Examples
- The following script
prints a file with the first field of each line moved to the end of
the line:
while read -r xx yy do print printf "%s %s/n" $yy $xx done < InputFile
- To read a line and split it into fields, and use
Please enter:
as a prompt, type:
The system displays:read word1?"Please enter: " word2
The value of the word1 variable should havePlease enter: You enter: hello world
hello
and word2 should haveworld
. - To create a co-process, then use print -p to
write to the co-process, and use read -p to read the input from the co-process, type:
The value of the line variable should have(read; print "hello $REPLY") print -p "world" read -p line
hello world
. - To save a copy of the input line as a command in the
history file, type:
If input_file containsread -s line < input_file
echo hello world
, thenecho hello world
will be saved as a command in the history file.