Bash user input with read explained banner
Bash focus guide

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.

On this page
Basics

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"
$ ./read-name.sh
Enter your name:
Robbie
Hello, Robbie
Important

Why use read -r?

Use read -r in most scripts. The -r option prevents backslashes from being treated as escape characters.

CommandUse
read NAMEWorks for simple input, but can treat backslashes specially.
read -r NAMESafer default for most scripts.
read -r -p "Name: " NAMEShows a prompt and reads input on one line.
Prompt

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-prompt.sh
Domain: example.com
You entered: example.com
Validation

Check 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"
$ ./check-domain-input.sh
Domain: example.com
Checking example.com
93.184.216.34

This uses the -z string test. For more test operators, see Bash Operators Explained.

Defaults

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"
$ ./default-service.sh
Service [nginx]:
Checking service: nginx
active

The expression ${SERVICE:-nginx} means “use nginx if SERVICE is empty”.

Confirmations

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
$ ./confirm-action.sh
Continue? [y/N] y
Continuing...
Defaulting to “no” is safer for scripts that change things. Your script should not YOLO a delete because someone pressed Enter too quickly.
Hidden input

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-input.sh
Password:
Password length: 12
Arguments or input?

When 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.

Downloads

Download the example scripts

Mistakes

Common Bash input mistakes

MistakeProblemBetter
read NAME everywhereBackslashes can be treated specially.read -r NAME
Not checking blank inputThe script may run with an empty variable.Use [ -z "$VALUE" ].
Using input prompts in automationScripts can hang waiting for input.Use arguments for cron or systemd jobs.
Unsafe confirmationsPressing Enter might accidentally continue.Default to cancel unless the user types yes.
Related

Keep learning Bash

Practical script

Example: safe confirmation prompt

For risky operations, ask for confirmation and only continue on a clear answer.

#!/usr/bin/env bash

read -r -p "Restart nginx now? Type yes to continue: " answer

if [ "$answer" != "yes" ]; then
  echo "Cancelled"
  exit 0
fi

echo "Restarting nginx..."
systemctl restart nginx
Example output:
$ ./restart-nginx.sh
Restart nginx now? Type yes to continue: no
Cancelled

$ ./restart-nginx.sh
Restart nginx now? Type yes to continue: yes
Restarting nginx...
Choosing input

Should you use read or script arguments?

UseBest for
readInteractive scripts, confirmations, menus and one-off prompts.
ArgumentsAutomation, cron jobs, repeatable scripts and commands run by other tools.
Config fileReusable settings such as domains, paths, thresholds and email addresses.

Related: Bash Arguments Explained and Bash Scripting Hub.

FAQ

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.

$ practise_next --topic bash

Practise this next

Turn the guide into practice with a related quiz, builder, cheat sheet or learning path.