read
Read a line from standard input
TLDR
Store data that you type from the keyboard
Store each of the next lines you enter as values of an array
Specify the number of maximum characters to be read
Assign multiple values to multiple variables
Do not let backslash (\\) act as an escape character
Display a prompt before the input
Do not echo typed characters (silent mode)
Read stdin and perform an action on every line
SYNOPSIS
read [OPTIONS] [VARIABLE...]
PARAMETERS
-a ARRAY
Assign words read to sequential indices of ARRAY, starting at zero.
-d DELIM
Continue reading until the first character of DELIM is encountered, rather than newline.
-e
Use Readline to obtain the line, providing editing features when reading from a terminal.
-n NCHARS
Return after reading at most NCHARS characters rather than waiting for a newline or DELIM.
-p PROMPT
Display PROMPT on standard error without a trailing newline before attempting to read.
-r
Backslash does not act as an escape character. The backslash is considered part of the line.
-s
Silent mode. Do not echo input coming from a terminal.
-t TIMEOUT
Cause read to terminate after TIMEOUT seconds if a complete line is not read.
-u FD
Read from file descriptor FD instead of standard input (0).
VARIABLE...
One or more shell variables to which the words of the input line will be assigned. If fewer VARIABLES are provided than words, the remaining words are assigned to the last VARIABLE. If no VARIABLES are provided, the entire line is stored in the REPLY variable.
DESCRIPTION
The read command is a shell built-in that reads a single line from standard input (or a specified file descriptor) and splits it into fields. These fields are then assigned sequentially to shell variables. By default, words are separated by whitespace (spaces, tabs, newlines), but this behavior can be altered using the IFS (Internal Field Separator) environment variable. It's an indispensable tool for interactive shell scripting, allowing scripts to prompt users for input or to process data line by line from files or pipes. The command can handle various scenarios, such as reading sensitive information without echoing it, setting a timeout for input, or reading only a specific number of characters. Its flexibility makes it a cornerstone for creating dynamic and interactive shell automation.
CAVEATS
The behavior of read is heavily influenced by the IFS (Internal Field Separator) environment variable, which determines how input lines are split into words. Care must be taken when setting or unsetting IFS to achieve desired parsing results. The command's return status indicates success (0) if a line was read, or failure (non-zero) if EOF was reached, a timeout occurred, or an error occurred. This status is crucial for loop control when processing files line by line. Be aware that the -s option only works when reading from a terminal; it has no effect when input is redirected from a pipe or file. Similarly, -p and -e are primarily useful for interactive terminal input.
RETURN STATUS
The read command returns a status of 0 upon successful completion (i.e., a line was read). It returns a non-zero status if an end-of-file is encountered, a timeout occurs (with -t), or an error occurs. This is critical for controlling loops and error handling in scripts.
IFS AND WORD SPLITTING
By default, read splits the input line into words based on the characters in the IFS (Internal Field Separator) environment variable. If IFS is unset or contains only whitespace, words are delimited by whitespace (spaces, tabs, newlines). Leading and trailing IFS whitespace is trimmed, and consecutive IFS characters are treated as a single delimiter. Understanding and manipulating IFS is key for precise parsing.
READING FROM FILES AND PIPES
While read defaults to standard input, it can read from files using input redirection (e.g., while read line; do ... done < file.txt) or from pipes (e.g., cat file.txt | while read line; do ... done). The -u option also allows reading from a specific file descriptor opened with exec.
HISTORY
The read command is a fundamental shell built-in, present in virtually all modern Unix-like shells, including bash, ksh, and zsh. Its core functionality of reading a line and assigning to variables has been a staple of shell scripting since early versions of the Bourne shell. Over time, various options like -p for prompts, -s for silent input, and -t for timeouts have been added to enhance its capabilities for interactive scripts and robust data processing. Its ubiquitous nature makes it one of the most frequently used commands in shell automation and user interaction.