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
-r
Raw mode. Backslash escapes are not interpreted.
-d delim
Use delim as the delimiter character, rather than newline. If empty, read will read until EOF.
-n nchars
Read at most nchars characters rather than one line.
-p prompt
Display prompt on standard error before reading input.
-t timeout
Timeout after timeout seconds. Returns failure if the complete input line is not read within timeout seconds. This option is only available when reading input from a terminal, pipe, or other special file.
-s
Silent mode. Do not echo input on the terminal. This is useful for reading passwords.
-a array
Read the words into the array named by array, starting with index 0. Only works if IFS is set.
-e
Use Readline to obtain the line. (Interactive editing)
variable...
The variables to which the input fields are assigned. If more words are read than variables supplied, the remaining words are assigned to the last variable.
DESCRIPTION
The read command in Linux is used to read a line from standard input and split it into fields. These fields are then assigned to the specified variables. It's a fundamental tool for creating interactive shell scripts and processing user input.
The command offers various options to customize its behavior, such as setting a timeout, specifying a prompt, reading only a certain number of characters, or hiding the input (e.g., for passwords). Understanding read is essential for anyone working with shell scripting as it allows you to create programs that can interact with the user and react to their input.
CAVEATS
When using `read` in a pipe, the variables set by `read` are not available in the parent shell, due to the subshell environment created by the pipe.
IFS (INTERNAL FIELD SEPARATOR)
The `IFS` variable determines how the input line is split into fields. By default, it includes space, tab, and newline characters. You can modify `IFS` to control how the `read` command splits the input into variables. For example, setting `IFS=:` will split the input based on colons.
EXIT STATUS
The `read` command returns an exit status of 0 if it successfully reads a line of input. It returns a non-zero exit status if an error occurs (e.g., a timeout), or if EOF is encountered before any characters are read.
HISTORY
The `read` command has been a standard part of Unix-like operating systems for many years. Its primary purpose has always been to facilitate user input and create interactive scripts. Over time, enhancements have been added, such as the `-t` (timeout) and `-s` (silent) options, to improve the command's versatility and security.