Name
read
Synopsis
read [options] [variable1
variable2 ...]Read one line of standard input and assign each word (as defined by IFS) to the corresponding variable, with all leftover words assigned to the last variable. If only one variable is specified, the entire line will be assigned to that variable. The return status is 0 unless EOF is reached, a distinction that is useful for running loops over input files. If no variable names are provided, read the entire string into the environment variable REPLY.
Options
- -a var
Read each word into an element of var, which is treated as an array variable.
- -d char
Stop reading the line at char instead of at the newline.
- -e
Line editing and command history are enabled during input.
- -n num
Read only num characters from the line.
- -p string
Display the prompt string to the user before reading each line, if input is interactive.
- -r
Raw mode; ignore \ as a line continuation character.
- -s
Do not echo the characters entered by the user (useful for reading a password).
- -t seconds
Time out and return without setting any variables if input is interactive and no input has been entered for seconds seconds.
- -u fd
Read input from specified file descriptor fd instead of standard input.
Examples
read first last addressSarah Caldwell 123 Main Street $echo "$last, $first\n$address"Caldwell, Sarah 123 Main Street
The following commands, which read a password into the variable $user_pw and then display its value, use recently added options that are not in all versions ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access