Quick take: The read command reads a line of input into one or more variables, making shell scripts interactive. read -p 'Name: ' name prompts and stores the answer; -s hides input for passwords and -t sets a timeout.
Introduction
The read command is the shell's way of accepting input — from a user at a prompt or from a file line by line. It is the building block of interactive scripts, letting you ask questions, capture answers, and read passwords without echoing them to the screen.
Syntax
The basic syntax of the read command is:
read [OPTIONS] [VARIABLE...]Common Options and Parameters
The most useful options and parameters for the read command:
| Option | Description |
|---|---|
| -p PROMPT | Display a prompt before reading. |
| -s | Silent — do not echo input (for passwords). |
| -t N | Time out after N seconds. |
| -n N | Return after reading N characters. |
| -a ARRAY | Read words into an array. |
| -r | Raw — do not treat backslashes as escapes (recommended). |
| -d DELIM | Use DELIM instead of newline to end input. |
Practical Examples
Real read commands you can run today:
# Prompt for a value
read -p 'Enter your name: ' name
# Read a password without echoing
read -sp 'Password: ' pass; echo
# Read with a 10-second timeout
read -t 10 -p 'Quick! ' answer
# Read several values at once
read -p 'First Last: ' first last
# Read a file line by line
while IFS= read -r line; do echo "$line"; done < file.txt
# Simple yes/no confirmation
read -p 'Continue? [y/N] ' ans; [[ $ans == y ]] && echo goTips and Best Practices
- Always use
-rwith read to stop backslashes being interpreted — the recommended form for reading data. - Use
-sfor passwords so the input is not shown, and addechoafterward to move to a new line. - The idiom
while IFS= read -r line; do ...; done < fileis the correct, whitespace-safe way to read a file line by line.
Final Thoughts
read makes shell scripts interactive — prompting for input, capturing answers, hiding passwords, and reading files line by line. Remember -p for prompts, -s for secrets, -t for timeouts, and always -r for safe raw input. The while IFS= read -r line idiom is the reliable way to process a file one line at a time.
FAQ: read Command in Linux
How do I read user input in a bash script?+
Use read -p 'Prompt: ' varname, which displays the prompt and stores the typed answer in varname. Then reference it as $varname.
How do I read a password without showing it?+
Use the -s (silent) flag: read -sp 'Password: ' pass. The input is hidden as the user types; follow with echo to print a newline.
How do I read a file line by line?+
Use the idiom while IFS= read -r line; do ...; done < file. Setting IFS= and using -r preserves leading whitespace and backslashes exactly.
Why should I use read -r?+
Without -r, read interprets backslashes as escape characters, which can corrupt input. The -r flag reads the line raw, which is almost always what you want.
How do I add a timeout to read?+
Use -t with seconds: read -t 10 -p 'Answer: ' var returns automatically after 10 seconds if no input is given, which is useful for prompts with a default.
Need help with Linux servers or infrastructure?
Work directly with Muhammad Irfan Aslam for Linux, Ubuntu, Docker, DevOps, cloud, CI/CD, or infrastructure support.
Hire Me for Support