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.
_10read -r -p "Project name [my-app]: " name_10name=${name:-my-app}_10_10read -r -s -p "API token: " token_10echo # move to a new line after hidden input_10_10echo "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, soC:\tempturns intoC:temp. There is almost never a reason to leave-rout. - Without
IFS=, leading and trailing spaces are trimmed. That is usually what you want for a prompt, but not when reading raw lines. -ponly shows the prompt when input comes from a terminal. If the script is fed from a pipe, the prompt is silently skipped andreadtakes 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
_10read -r -p "Port [3000]: " port_10port=${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:
_10read -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
_10read -r -s -p "Password: " password_10echo_10_10if [[ -z $password ]]; then_10 echo "A password is required" >&2_10 exit 1_10fi
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
_10read -r -p "Delete the build folder? [y/N] " answer_10_10case $answer in_10 [yY] | [yY][eE][sS])_10 rm -rf build_10 ;;_10 *)_10 echo "Skipped"_10 ;;_10esac
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
_10while 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_10done
Add a timeout
_10if read -r -t 10 -p "Continue? [Y/n] " answer; then_10 answer=${answer:-y}_10else_10 echo_10 answer=y # no reply within 10 seconds_10fi
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:
_10read -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:
_10name=${1:-${PROJECT_NAME:-}}_10if [[ -z $name && -t 0 ]]; then_10 read -r -p "Project name: " name_10fi
-t 0 is true when standard input is a terminal.
Common pitfalls
#- Leaving out
-r****. Backslashes in the input disappear. - Using
echo -nfor the prompt. It works, but-pis shorter and keeps the prompt on standard error, so it does not end up in redirected output. - Running the script with
sh****.-pand-sare Bash options. Under dash,read -pfails. - Unquoted variables afterwards. Users type spaces. Use
"$name"when you pass the value on. - Reading several values at once.
read -r first lastputs the first word infirstand everything else inlast, which surprises people with three-part names.
Related reading
#Was this resource helpful?