read

From RaySoft

Read a line from the standard input and split it into fields.[1]

Documentation

Syntax

read [PARAMETER ...] [NAME ...]

Parameters

-a ARRAY
Assign the words read to sequential indices of the array variable ARRAY.
-d DELIMITER
Continue until the first character of DELIMITER is read, rather than newline.
-e
Use Readline to obtain the line in an interactive shell.
-N NUMBER
Return only after reading exactly NUMBER of characters, unless EOF is encountered or read times out, ignoring any delimiter.
-p PROMPT
Output the string PROMPT without a trailing newline before attempting to read.
-r
Do not allow backslashes to escape any characters.
-s
Do not echo input coming from a terminal.
-t TIME
Time out and return failure if a complete line of input is not read within TIME seconds. The value of the TMOUT variable is the default timeout. TIME may be a fractional number. If TIME is 0, read returns success only if input is available on the specified file descriptor. The exit status is greater than 128 if the timeout is exceeded.

Examples

Get a password[2]
read -p 'Password: ' -r -s password
Get a yes or no answer[2]
read -N 1 -p 'Y or N: ' -r answer
Read each field of a file
while IFS=':' read -r user passwd uid gid info home shell; do
   echo "User:     ${user}"
   echo "User ID:  ${uid}"
   echo "Group ID: ${gid}"
   echo "Info:     ${info}"
   echo "Home Dir: ${home}"
   echo "Shell:    ${shell}"
   echo
done <'/etc/passwd'
Provide an opportunity to leave the script
read -n 1 -p 'Press Ctrl-C to exit or any key to proceed.' -r -s

References