read Command
Intermediate Shell Scripting man(1)Read a line of input from standard input
👁 8 views
📅 Updated: Mar 15, 2026
SYNTAX
read [OPTION]... [NAME]...
What Does read Do?
The read command reads a line of input from standard input (keyboard or pipe) and assigns it to one or more variables. It is the primary way to get user input in bash scripts.
read supports prompts, timeouts, silent mode (for passwords), character limits, and reading into arrays. It can read from files line by line in while loops, making it essential for processing text data.
read is a shell built-in in bash and other Bourne-compatible shells. It blocks execution until input is received (unless a timeout is set), making it useful for interactive scripts and confirmation prompts.
read supports prompts, timeouts, silent mode (for passwords), character limits, and reading into arrays. It can read from files line by line in while loops, making it essential for processing text data.
read is a shell built-in in bash and other Bourne-compatible shells. It blocks execution until input is received (unless a timeout is set), making it useful for interactive scripts and confirmation prompts.
Options & Flags
| Option | Description | Example |
|---|---|---|
| -p | Display a prompt string | read -p "Enter name: " name |
| -s | Silent mode (for passwords) | read -sp "Password: " pass |
| -t | Timeout in seconds | read -t 10 -p "Quick! " answer |
| -n | Read only N characters | read -n 1 -p "Continue? [y/n] " choice |
| -r | Raw mode (do not interpret backslashes) | read -r line |
| -a | Read into an array | read -a words |
| -d | Set delimiter instead of newline | read -d',' field |
Practical Examples
#1 Simple input
Prompts for input and stores it in the variable $name.
$ read -p "Enter your name: " name; echo "Hello, $name"#2 Password input
Reads input without displaying characters. The echo adds a newline after.
$ read -sp "Password: " pass; echo#3 Yes/No confirmation
Reads a single character for quick yes/no confirmation.
$ read -n 1 -p "Continue? [y/n] " answer; [[ "$answer" == "y" ]] && echo " Yes!" || echo " No!"#4 Read with timeout
Waits 5 seconds for input, then continues with a timeout message.
$ read -t 5 -p "Answer within 5 seconds: " answer || echo "Timeout!"#5 Read file line by line
Processes each line of a file — the standard file reading pattern in bash.
$ while IFS= read -r line; do echo "Line: $line"; done < file.txt#6 Multiple variables
Splits input into multiple variables by whitespace.
$ echo "Alice 30 London" | read name age city; echo "$name is $age in $city"#7 Read into array
Reads space-separated words into an array.
$ read -a colors -p "Enter colors: "; echo "First: ${colors[0]}"Tips & Best Practices
Always use -r: Use read -r to prevent backslash interpretation. Without -r, backslashes are treated as escape characters, which can corrupt data.
IFS for custom splitting: Set IFS to split by custom delimiter: IFS=':' read -r user pass uid gid info home shell < /etc/passwd
Pipe creates subshell: echo "data" | read var — the read runs in a subshell, so $var is empty after. Use: read var <<< "data" or process substitution.
Frequently Asked Questions
How do I read user input in bash?
Use read -p "Prompt: " variable. The input is stored in $variable for use later in the script.
How do I read a password without showing it?
Use read -sp "Password: " pass. The -s flag hides input. Add echo after to print a newline.
How do I read a file line by line?
Use: while IFS= read -r line; do echo "$line"; done < file.txt. Always use -r to preserve backslashes.
Related Commands
More Shell Scripting Commands
Master Linux with Professional eBooks
Curated IT eBooks covering Linux, DevOps, Cloud, and more
Browse Books →