---
title: "How do I ask the user for input in a Bash script?"
description: "Use read -r -p to prompt for input in Bash, -s to hide passwords, and parameter expansion for default values. Covers yes or no prompts, timeouts and piped input."
url: https://www.danny.engineering/answer/bash/how-do-i-ask-the-user-for-input-in-a-bash-script
author: Danny
published: 2026-11-27T00:00:00.000Z
updated: 2026-11-27T20:30:00.000Z
tags: ["Bash"]
---

# How do I ask the user for input in a Bash script?

## The short answer


Use the `read` built-in with `-r` so backslashes are kept and `-p` to show a prompt. Add `-s` to hide what the user types, and use `${var:-default}` to fall back to a default when they just press Enter.


```bash
read -r -p "Project name [my-app]: " name
name=${name:-my-app}

read -r -s -p "API token: " token
echo   # move to a new line after hidden input

echo "Creating $name"
```


## Why it happens


`read` reads one line from standard input and stores it in the variables you name. A few defaults make it awkward if you use it bare:

- Without `-r`, a backslash is treated as an escape character, so `C:\temp` turns into `C:temp`. There is almost never a reason to leave `-r` out.
- Without `IFS=`, leading and trailing spaces are trimmed. That is usually what you want for a prompt, but not when reading raw lines.
- `-p` only shows the prompt when input comes from a terminal. If the script is fed from a pipe, the prompt is silently skipped and `read` takes its value from the pipe instead.
- With `-s`, the newline the user types is not echoed either, so the next output appears on the same line as the prompt.

## How to fix it


### Prompt with a default value


```bash
read -r -p "Port [3000]: " port
port=${port:-3000}
```


`${port:-3000}` uses `3000` when `port` is empty or unset. If you are on Bash 4 or later, you can also pre-fill the input so the user can edit it, using `-e` (readline) and `-i`:


```bash
read -r -e -i "3000" -p "Port: " port
```


The `-i` option is not available in the Bash 3.2 that ships with macOS, so the parameter expansion approach is the portable choice.


### Hide passwords and secrets


```bash
read -r -s -p "Password: " password
echo

if [[ -z $password ]]; then
  echo "A password is required" >&2
  exit 1
fi
```


Hiding input stops it showing on screen, but the value still lives in a shell variable. Do not echo it, and pass it to other programs through standard input or an environment variable rather than as a command-line argument, which other users can see in the process list.


### Ask a yes or no question


```bash
read -r -p "Delete the build folder? [y/N] " answer

case $answer in
  [yY] | [yY][eE][sS])
    rm -rf build
    ;;
  *)
    echo "Skipped"
    ;;
esac
```


Make the safe option the default, and show it in capitals in the prompt. For a single key press without Enter, add `-n 1`.


### Keep asking until the input is valid


```bash
while true; do
  read -r -p "How many workers (1-16)? " workers
  if [[ $workers =~ ^[0-9]+$ ]] && (( workers >= 1 && workers <= 16 )); then
    break
  fi
  echo "Please enter a number from 1 to 16" >&2
done
```


### Add a timeout


```bash
if read -r -t 10 -p "Continue? [Y/n] " answer; then
  answer=${answer:-y}
else
  echo
  answer=y   # no reply within 10 seconds
fi
```


`read` returns a non-zero status when it times out or reaches the end of input, so you can test it in an `if`.


### Read from the keyboard when stdin is busy


Inside a `while read` loop, or in a script run as `curl ... | bash`, standard input is not the keyboard. Read from the terminal directly:


```bash
read -r -p "Continue? [y/N] " answer < /dev/tty
```


### Do not prompt in automation


Scripts that run in CI or cron have no one to answer. Accept the value from an argument or environment variable first, and only prompt when it is missing and a terminal is attached:


```bash
name=${1:-${PROJECT_NAME:-}}
if [[ -z $name && -t 0 ]]; then
  read -r -p "Project name: " name
fi
```


`-t 0` is true when standard input is a terminal.


## Common pitfalls

- **Leaving out** **`-r`****.** Backslashes in the input disappear.
- **Using** **`echo -n`** **for the prompt.** It works, but `-p` is shorter and keeps the prompt on standard error, so it does not end up in redirected output.
- **Running the script with** **`sh`****.** `-p` and `-s` are Bash options. Under dash, `read -p` fails.
- **Unquoted variables afterwards.** Users type spaces. Use `"$name"` when you pass the value on.
- **Reading several values at once.** `read -r first last` puts the first word in `first` and everything else in `last`, which surprises people with three-part names.

## Related reading

- [GNU Bash manual: Bash builtins (read)](https://www.gnu.org/software/bash/manual/html_node/Bash-Builtins.html)
- [GNU Bash manual: Shell parameter expansion](https://www.gnu.org/software/bash/manual/html_node/Shell-Parameter-Expansion.html)
