read — Reads a line of input into shell variables
read
[-d {delimiter
}] [-e] [-n {number
}] [-p {prompt
}] [-s] [-t {timeout
}] [-u {file
}] {variable
...}
-d delimiter
The delimiter is a single character which is used to terminate the input line the default if this option is not given is newline.
-e
If the input is not coming from a file
(-u
isn't used) the input will be
read with a line editor. This line editor is the same
as the main shell but with no history.
-n number
The command exits after reading the specified number of characters.
-p prompt
The prompt will be output before starting to
accept input. The prompt is only displayed if
-u
is not being used.
-s
If the -u
is not being used
characters entered are usually displayed, this switch
inhibits this display. This can be used for password
entry.
-t timeout
this stops reading input after the timeout and
exits with a non zero exit code. The option has no
effect if the -u
option is being
used.
-u fd
Instead of reading input from the console read from the given file descriptor.
variable
The names of the variables to place the read text
into. If no names are supplied the input is put into
the REPLY
variable.
One line is read from the console, or from the file descriptor supplied as an argument to the -u option, and the first word is assigned to the first variable name, the second word to the second name, and so on, with leftover words and their intervening separators assigned to the last name. If there are fewer words read from the input stream than names, the remaining names are assigned empty values.
The characters in IFS
are used to
split the line into words unless the delimiter switch is
used.
The return code is zero, unless end-of-file is
encountered, read times out, or an invalid file is
supplied as the argument to -u
.
Example 49. Using the read command
This example shows the use to the read command in several ways. Firstly reading simple text without a prompt and then with a long prompt.
>read some text >echo $REPLY some text >read -p "a long prompt $" a long prompt $some text with a long prompt >echo $REPLY some text with a long prompt >
This shows reading values into a specified variable
and how the REPLY
variable is unaffected.
>REPLY="no reply" >read -p $ VARIABLE $some text with a prompt in VARIABLE >echo $VARIABLE some text with a prompt in VARIABLE >echo $REPLY no reply >
This shows the use of the silent switch to suppress echoing of output.
>read -s VARIABLE >echo $VARIABLE some text in VARIABLE with silent enabled >
This shows the use of the delimiter and timeout options to read a preset number of characters and perform a read within a set amount of time.
>read -n 5 VARIABLE 12345>echo $VARIABLE 12345 >read -d 9 VARIABLE this text continues until a 9>echo $VARIABLE this text continues until a >echo $? 0 >read -t 3 VARIABLE >echo $? 1 >echo $VARIABLE this text continues until a >