read
read is a built-in command that reads input from standard input (keyboard) or line data into variables. Use it to display interactive prompts for the user or to process a text file line by line.
Syntax
Read standard input into a variable.
read variable_name
Use the -p option to display a prompt and accept input.
read -p "Enter input: " variable_name
Use the -s option to hide input as it is typed (useful for passwords).
read -s -p "Password: " variable_name
Use the -a option to read input into an array.
read -a array_name
Process a file line by line with a while loop.
while read line; do
echo "$line"
done < filename
Options
| Option | Description |
|---|---|
| -p "prompt" | Displays a prompt string before reading input. |
| -s | Silent mode — input is not echoed to the screen (useful for passwords). |
| -r | Treats backslashes literally instead of as escape characters (recommended when reading files). |
| -a array_name | Reads space-separated input into an array. |
| -n count | Stops reading automatically after the specified number of characters. |
| -t timeout | Times out after the specified number of seconds if no input is received. |
| -d delimiter | Uses the specified character as the input terminator instead of a newline. |
| -u fd | Reads from the specified file descriptor. |
Sample Code
Use the -p option to display a prompt and accept input.
read_name.sh
read -p "Enter your name: " name
echo "Hello, ${name}!"
bash read_name.sh
Enter your name: Alice
Hello, Alice!
Use the -s option to hide input as it is typed. This is useful for password entry.
read_password.sh
read -s -p "Password: " password
echo ""
echo "Password accepted (length: ${#password})"
bash read_password.sh
Password:
Password accepted (length: 8)
Use the -t option to set a timeout. If no input is received within the specified number of seconds, the command returns exit status 1.
read_timeout.sh
read -t 10 -p "Continue? [y/N]: " answer
if [ $? -ne 0 ]; then
echo "Timed out. Aborting."
exit 1
fi
[ "$answer" = "y" ] || { echo "Aborted."; exit 0; }
bash read_timeout.sh
Continue? [y/N]: y
Use the -a option to read space-separated input into an array.
read_array.sh
read -p "Enter fruits (space-separated): " -a fruits
echo "First: ${fruits[0]}"
echo "All: ${fruits[@]}"
bash read_array.sh
Enter fruits (space-separated): apple banana cherry
First: apple
All: apple banana cherry
Read a file line by line. Use IFS= to preserve leading and trailing whitespace, and -r to prevent backslash interpretation.
read_file.sh
while IFS= read -r line; do
echo "Line: $line"
done < /etc/hosts
bash read_file.sh
Line: 127.0.0.1 localhost
Line: ::1 localhost
You can process piped input the same way.
read_pipe.sh
ls /var/log/*.log | while read -r logfile; do
size=$(wc -l < "$logfile")
echo "$logfile: ${size} lines"
done
bash read_pipe.sh
/var/log/syslog.log: 1520 lines
/var/log/auth.log: 340 lines
Notes
When reading a file line by line, while IFS= read -r line is the standard idiom. IFS= preserves leading and trailing whitespace, and -r prevents backslash sequences from being interpreted as escape characters.
The -t option sets a timeout so a script does not hang waiting for input indefinitely. Combining -s and -p for password input is a common pattern in installer scripts. For more on loops, see while / until.
If you find any errors or copyright issues, please contact us.