read
| Since: | All Linux | |
|---|---|---|
| macOS(2001 Cheetah) | ||
| Bash 1.0(1989) |
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.
sample_read_name.sh
read -p "Enter your name: " name
echo "Hello, ${name}!"
Run the following command:
bash read_name.sh Enter your name: Yagami Iori Hello, Yagami Iori!
Use the -s option to hide input as it is typed. This is useful for password entry.
sample_read_password.sh
read -s -p "Password: " password
echo ""
echo "Password accepted (length: ${#password})"
Run the following command:
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.
sample_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; }
Run the following command:
bash read_timeout.sh Continue? [y/N]: y
Use the -a option to read space-separated input into an array.
sample_read_array.sh
read -p "Enter members (space-separated): " -a members
echo "First: ${members[0]}"
echo "All: ${members[@]}"
Run the following command:
bash read_array.sh Enter members (space-separated): Iori Kyo Terry First: Iori All: Iori Kyo Terry
Read a file line by line. Use IFS= to preserve leading and trailing whitespace, and -r to prevent backslash interpretation.
sample_read_file.sh
while IFS= read -r line; do
echo "Line: $line"
done < /etc/hosts
Run the following command:
bash read_file.sh Line: 127.0.0.1 localhost Line: ::1 localhost
You can process piped input the same way.
sample_read_pipe.sh
ls /var/log/*.log | while read -r logfile; do
size=$(wc -l < "$logfile")
echo "$logfile: ${size} lines"
done
Run the following command:
bash read_pipe.sh /var/log/syslog.log: 1520 lines /var/log/auth.log: 340 lines
Common Mistakes
Common Mistake 1: Without -r, backslashes in the input are consumed
Without -r, read interprets backslash sequences. A line ending with \ is joined with the next line.
echo "path\\to\\file" | while read line; do echo "$line"; done path\to\file (backslash pairs are collapsed)
Always use -r to read input literally.
echo "path\\to\\file" | while read -r line; do echo "$line"; done path\\to\\file
Common Mistake 2: Without IFS=, leading and trailing whitespace is stripped
By default, read strips leading and trailing whitespace from each line. This can alter data that uses significant spaces.
echo " hello " | while read line; do echo "[$line]"; done [hello] (leading and trailing spaces removed)
Set IFS= before read to preserve all whitespace.
echo " hello " | while IFS= read -r line; do echo "[$line]"; done [ hello ]
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.