Skip to content
Tutorials Center
My learning

Topics

Linux Command Reference ยท Shell Builtins

Bash read command

Reference for the Bash read builtin: split a line into variables, use -r, -p, -s, -t, -n, -d, -a and IFS, and loop over files and pipes safely.

read takes one line from standard input (or another file descriptor), splits it into words and assigns them to variables. It is the standard way to ask a user a question, to process a file line by line and to take a pipeline's output apart.

How the line is split depends on IFS, the "input field separator". By default it holds space, tab and newline, so read first rest puts the first word into first and everything else into rest.

Terminal
read [-ers] [-a ARRAY] [-d DELIM] [-i TEXT] [-n N] [-N N] [-p PROMPT] [-t SECONDS] [-u FD] [NAME ...]

Try it

Bash Splitting a line into variables

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Bash Reading files, timeouts and exit status

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Syntax

Terminal
read [-ers] [-a array] [-d delim] [-i text] [-n nchars] [-N nchars] [-p prompt] [-t timeout] [-u fd] [name ...]
Name Description
name ... Variables that receive the words. Extra words go into the last one. With no names, the whole line is stored in REPLY.
-r Raw: do not treat backslash as an escape and do not join continued lines. Almost always what you want.
-a array Store the words in an indexed array, starting at index 0.
-d delim Stop at the first character of delim instead of a newline. -d '' reads up to a NUL byte.
-n N Return after reading at most N characters (or earlier at a delimiter).
-N N Read exactly N characters and ignore delimiters.
-p prompt Print the prompt before reading (only when input comes from a terminal).
-s Silent: do not echo typed characters (passwords).
-t seconds Time out and return a status above 128 if no full input arrives. Fractions are allowed from Bash 4.
-u fd Read from file descriptor fd instead of standard input.
-e Use readline for editing (interactive).
-i text With -e, the initial text to edit.

Returns: 0 when a line was read; 1 at end of file (the variables are still set from the partial last line); greater than 128 on timeout; greater than 1 for an invalid file descriptor.

Errors and edge cases: on timeout, partial input is stored in the variables in Bash 4 and later. With set -e, a plain read at end of file aborts the script.

Usage notes

  • Always IFS= read -r when reading lines: IFS= stops leading and trailing whitespace from being stripped, and -r stops backslash processing. Omitting them changes the data.
  • read in a pipeline runs in a subshell, so cmd | read x loses x. Use process substitution (read x < <(cmd)), a here-string, or shopt -s lastpipe in scripts.
  • Missing final newline. A file that does not end in newline makes read return 1 for the last line while still assigning it, so loops that test only the status skip it. The || [ -n "$line" ] idiom handles it.
  • Standard input conflicts. A while read loop reading a file can swallow input meant for commands inside the loop (like ssh). Redirect them with < /dev/null or read the file on another descriptor (while read -u 3 ...; done 3< file).
  • Passwords. read -s -p "Password: " pw hides typing; remember to print a newline afterwards with echo. The value still lives in shell memory.
  • Menus and keys. read -n 1 -r key reacts to a single keypress; -t combined with it makes a countdown prompt.
  • Counting. For splitting a whole file into an array use mapfile; for formatted output see printf.
  • IFS characters that are whitespace collapse (a b is two words), while non-whitespace characters do not (a::b has an empty middle field).