More Bash answers

by Danny
Published 2 min read

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.

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.


_10
read -r -p "Project name [my-app]: " name
_10
name=${name:-my-app}
_10
_10
read -r -s -p "API token: " token
_10
echo # move to a new line after hidden input
_10
_10
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


_10
read -r -p "Port [3000]: " port
_10
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:


_10
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


_10
read -r -s -p "Password: " password
_10
echo
_10
_10
if [[ -z $password ]]; then
_10
echo "A password is required" >&2
_10
exit 1
_10
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


_10
read -r -p "Delete the build folder? [y/N] " answer
_10
_10
case $answer in
_10
[yY] | [yY][eE][sS])
_10
rm -rf build
_10
;;
_10
*)
_10
echo "Skipped"
_10
;;
_10
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


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

Add a timeout


_10
if read -r -t 10 -p "Continue? [Y/n] " answer; then
_10
answer=${answer:-y}
_10
else
_10
echo
_10
answer=y # no reply within 10 seconds
_10
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:


_10
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:


_10
name=${1:-${PROJECT_NAME:-}}
_10
if [[ -z $name && -t 0 ]]; then
_10
read -r -p "Project name: " name
_10
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.
#

Was this resource helpful?