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.
read [-ers] [-a ARRAY] [-d DELIM] [-i TEXT] [-n N] [-N N] [-p PROMPT] [-t SECONDS] [-u FD] [NAME ...]
Try it
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.
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
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 -rwhen reading lines:IFS=stops leading and trailing whitespace from being stripped, and-rstops backslash processing. Omitting them changes the data. readin a pipeline runs in a subshell, socmd | read xlosesx. Use process substitution (read x < <(cmd)), a here-string, orshopt -s lastpipein scripts.- Missing final newline. A file that does not end in newline makes
readreturn 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 readloop reading a file can swallow input meant for commands inside the loop (likessh). Redirect them with< /dev/nullor read the file on another descriptor (while read -u 3 ...; done 3< file). - Passwords.
read -s -p "Password: " pwhides typing; remember to print a newline afterwards withecho. The value still lives in shell memory. - Menus and keys.
read -n 1 -r keyreacts to a single keypress;-tcombined with it makes a countdown prompt. - Counting. For splitting a whole file into an array use
mapfile; for formatted output seeprintf. - IFS characters that are whitespace collapse (
a bis two words), while non-whitespace characters do not (a::bhas an empty middle field).
Related
mapfile,declare,printf -v,set- Lessons: Reading Input, Loops, Here Documents