
Bash User Input with read Explained
The Bash read command lets a script ask the user for input while it runs. Use it for prompts, confirmations, simple menus, safe checks and interactive admin scripts.
Basic Bash read example
The simplest input script asks for a value, stores it in a variable, then uses that variable.
#!/usr/bin/env bash
echo "Enter your name:"
read -r NAME
echo "Hello, $NAME"
Enter your name:
Robbie
Hello, RobbieWhy use read -r?
Use read -r in most scripts. The -r option prevents backslashes from being treated as escape characters.
| Command | Use |
|---|---|
read NAME | Works for simple input, but can treat backslashes specially. |
read -r NAME | Safer default for most scripts. |
read -r -p "Name: " NAME | Shows a prompt and reads input on one line. |
Prompt the user on one line
Use -p to show the prompt before reading input.
#!/usr/bin/env bash
read -r -p "Domain: " DOMAIN
echo "You entered: $DOMAIN"
Domain: example.com
You entered: example.comCheck that input was provided
Always check important input before using it.
#!/usr/bin/env bash
read -r -p "Domain: " DOMAIN
if [ -z "$DOMAIN" ]; then
echo "No domain entered"
exit 1
fi
echo "Checking $DOMAIN"
dig +short "$DOMAIN"
Domain: example.com
Checking example.com
93.184.216.34This uses the -z string test. For more test operators, see Bash Operators Explained.
Use a default value if input is blank
You can let the user press Enter to accept a default.
#!/usr/bin/env bash
read -r -p "Service [nginx]: " SERVICE
SERVICE="${SERVICE:-nginx}"
echo "Checking service: $SERVICE"
systemctl is-active "$SERVICE"
Service [nginx]:
Checking service: nginx
activeThe expression ${SERVICE:-nginx} means “use nginx if SERVICE is empty”.
Create a yes/no confirmation prompt
Confirmations are useful before running commands that change files, restart services or delete data.
#!/usr/bin/env bash
read -r -p "Continue? [y/N] " ANSWER
case "$ANSWER" in
y|Y|yes|YES)
echo "Continuing..."
;;
*)
echo "Cancelled."
exit 1
;;
esac
Continue? [y/N] y
Continuing...Read hidden input with -s
read -s hides what the user types. This is useful for passwords or tokens, though you should still avoid storing secrets carelessly.
#!/usr/bin/env bash
read -r -s -p "Password: " PASSWORD
echo
echo "Password length: ${#PASSWORD}"
Password:
Password length: 12When should you use arguments instead of read?
| Use arguments when... | Use read when... |
|---|---|
| The script should run unattended or from cron/systemd. | The script is interactive and a human is present. |
You want repeatable commands like ./check-service.sh nginx. | You want a guided prompt or menu. |
| You need automation. | You need confirmation before a risky action. |
For non-interactive scripts, arguments are usually better. For guided beginner tools, read is often easier.
See also: Bash Script Arguments Explained.
Download the example scripts
Common Bash input mistakes
| Mistake | Problem | Better |
|---|---|---|
read NAME everywhere | Backslashes can be treated specially. | read -r NAME |
| Not checking blank input | The script may run with an empty variable. | Use [ -z "$VALUE" ]. |
| Using input prompts in automation | Scripts can hang waiting for input. | Use arguments for cron or systemd jobs. |
| Unsafe confirmations | Pressing Enter might accidentally continue. | Default to cancel unless the user types yes. |
Keep learning Bash
Should you use read or script arguments?
| Use | Best for |
|---|---|
read | Interactive scripts, confirmations, menus and one-off prompts. |
| Arguments | Automation, cron jobs, repeatable scripts and commands run by other tools. |
| Config file | Reusable settings such as domains, paths, thresholds and email addresses. |
Related: Bash Arguments Explained and Bash Scripting Hub.
Frequently Asked Questions
What does read -r do in Bash?
read -r reads input without treating backslashes as escape characters, which is usually safer.
How do I prompt a user in Bash?
Use read -r -p "Prompt text: " variable to show a prompt and store the answer.
Should I use read in cron scripts?
No. Cron scripts are non-interactive, so use arguments, environment variables or config files instead.
How do I hide password input in Bash?
Use read -r -s -p "Password: " password, then print a newline after the prompt.