read command in Unix/Linux
March 20, 2012 Leave a comment
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 Variable Name 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 Variable Name parameter is assigned the value of the first field, the second shell variable specified by the Variable Name 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 Variable Name 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 Variable Name parameter, the variable REPLY is used as the default variable name.
–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.
To save a copy of the input line as a command in the history file, type:
read -s line < input_file
If input_file contains “echo hello world,” then “echo hello world” will be saved as a command in the history file.