LinuxCommandLibrary

read

TLDR

Read a line into a variable

$ read [variable]
copy
Read with a custom prompt
$ read -p "Enter your name: " [name]
copy
Read silently (for passwords)
$ read -s -p "Password: " [password]
copy
Read with a timeout (5 seconds)
$ read -t 5 [variable]
copy
Read a single character without waiting for Enter
$ read -n 1 [char]
copy
Read into an array (words split by IFS)
$ read -a [array]
copy
Read from a file line by line
$ while read line; do echo "$line"; done < [file]
copy

SYNOPSIS

read [-p prompt] [-t timeout] [-n nchars] [-s] [-a array] [-d delim] [name...]

DESCRIPTION

read is a shell builtin that reads a line from standard input (or a file descriptor) and splits it into words, assigning them to variables. It is fundamental to interactive shell scripts and processing text files.
Without variable names, input is stored in the REPLY variable. With multiple variables, words are assigned in order, with remaining words going to the last variable. Words are split according to the IFS (Internal Field Separator) variable.
The -r option is recommended for most uses as it prevents backslash interpretation, which can cause unexpected behavior with file paths or special characters.
In while read loops, read returns false (exit status 1) at end-of-file, making it ideal for processing files line by line.

PARAMETERS

-p prompt

Display prompt string before reading (bash)
-s
Silent mode; do not echo input (for passwords)
-t timeout
Timeout after specified seconds; fail if no input
-n nchars
Return after reading specified number of characters
-N nchars
Read exactly N characters, ignoring delimiters
-a array
Read words into array variable
-d delim
Use specified delimiter instead of newline
-r
Do not treat backslash as escape character (raw mode)
-u fd
Read from file descriptor instead of stdin
-e
Use readline for input (enables line editing)

CAVEATS

read is a shell builtin with varying options between shells (bash, zsh, dash). Options like -p, -a, and -t are bash extensions not available in POSIX sh.
Without -r, backslashes are interpreted as escape characters. Always use read -r unless you specifically need escape processing.
Leading and trailing whitespace is stripped based on IFS. To preserve whitespace, set IFS= before read: `IFS= read -r line`
The -t timeout option may not work in all shells or when reading from regular files (only terminals/pipes).

SEE ALSO

echo(1), printf(1), bash(1), sh(1)

Copied to clipboard