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 is 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
charinstead of at the newline.- -e
Line editing and command history are enabled during input.
- -n
num Read only
numcharacters from the line.- -p
string Display the prompt
stringto the user before reading each line, if input is interactive.- -r
Raw mode; ignore \ as a line continuation character.
- -s
Don’t 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
secondsseconds.- -u
fd Read input from specified file descriptor
fdinstead 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 of
bash ...
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