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
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
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
seconds
seconds.-
-u
fd
Read input from specified file descriptor
fd
instead of standard input.
Examples
$read first last address
Sarah 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 ...
Get Mac OS X Tiger in a Nutshell now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.