O'Reilly logo

Linux in a Nutshell, Fourth Edition by Aaron Weber, Stephen Figgins, Ellen Siever

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required