Name

read

Synopsis

    read [options] [variable1[?string]] [variable2 ...]

Read one line of standard input and assign each word 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. See the Examples here and under case. The return status is 0 unless EOF is reached. Both Bash and the Korn shell support options, as shown below. If no variables are given, input is stored in the REPLY variable.

Additionally, the Korn shell version supports the ? syntax for prompting. If the first variable is followed by ? string, string is displayed as a user prompt.

Options

-a array

Read into indexed array array. Bash only.

-A array

Read into indexed array array. ksh93 only.

-d delim

Read up to first occurrence of delim, instead of newline. Not ksh88.

-e

Use the readline library if reading from a terminal. Bash only.

-n count

Read at most count bytes. Not ksh88.

-p prompt

Bash: print prompt before reading input.

-p

Korn shell: read from the output of a |& coprocess.

-r

Raw mode; ignore \ as a line-continuation character.

-s

Bash: read silently; characters are not echoed.

-s

Korn shell: save input as a command in the history file.

-t timeout

When reading from a terminal or pipe, if no data is entered after timeout seconds, return 1. This prevents an application from hanging forever, waiting for user input. Not ksh88.

-u [ n ]

Read input from file descriptor n (default is 0).

Examples

Read three variables: ...

Get Unix in a Nutshell, 4th Edition 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.