Name

read

Synopsis

read [options] [variable1 [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. If no variables are given, input is stored in the REPLY variable.

Options

-a array

Read into indexed array array.

-d delim

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

-e

Use the readline library if reading from a terminal.

-n count

Read at most count bytes.

-p prompt

Print prompt before reading input.

-r

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

-s

Read silently; characters are not echoed.

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

-u[n]

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

Examples

Read three variables:

$ read first last address
Sarah Caldwell 123 Main Street

$ echo "$last, $first\n$address"
Caldwell, Sarah
123 Main Street

Prompt yourself to enter two temperatures:

$ read -p "High low: " n1 n2
High low: 65 33

Get bash Quick Reference 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.