read
Read a line from standard input
Overview
Reads a single line from standard input (or from a file descriptor specified with -u) and splits it into fields, assigning them to shell variables. Primarily used in shell scripts for user input.
Syntax
read [OPTIONS] [VARNAME1 VARNAME2 ...]Common Options
-rRaw mode. Do not allow backslashes to act as escape characters. Highly recommended.
-p PROMPTDisplay PROMPT on standard error, without a trailing newline, before attempting to read input.
-sSilent mode. If input is from a terminal, characters are not echoed. Useful for passwords.
-n NUMCHARSReturn after reading NUMCHARS characters rather than waiting for a newline.
-N NUMCHARSReturn after reading exactly NUMCHARS characters, ignoring delimiters, unless EOF or read error occurs.
-t TIMEOUTTime out and return failure if a complete line of input is not read within TIMEOUT seconds.
-a ARRAYNAMEAssign the words read to sequential indices of the array variable ARRAYNAME, starting at zero.
-d DELIMContinue until the first character of DELIM is read, rather than newline.
-u FDRead input from file descriptor FD.
VARNAMEsOne or more shell variable names. If no VARNAMEs are given, the line read is stored in the `REPLY` variable.
Examples
Reads a line from input and stores it in the variable `name`.
Prompts the user and stores their input in `color`.
Reads a password silently into `secret_pass`.
Pipes a string to `read`, assigning words to `fruit1`, `fruit2`, and `fruit3` (Note: in a subshell, variables may not persist as expected in all shells without workarounds).
Reads the first line from `data.txt` into `line` without interpreting backslashes.
Reads a file `employees.csv` line by line, preserving leading/trailing whitespace in each line.
Reads a single character for a yes/no prompt.
Waits up to 5 seconds for input.
Reads words from a here-string into the array `words`. (`echo ${words[1]}` would output `two`).