Confirmation prompt yes/no in Bash

Asking for user confirmation in a bash script is easy!

Yes/No prompt with default no

If the default is No for an invalid character or space, simply check if the prompt answer is y. Using read, user input can be limited to a single character using the -n 1 parameter.

1
2
3
4
5
read -r -p "Are you sure? [y/N]" -n 1
echo # (optional) move to a new line
if [[ "$REPLY" =~ ^[Yy]$ ]]; then
    echo "Operation continues"
fi

Similarly, a case statement may be used:

1
2
3
4
5
6
read -r -p "Are you sure? [y/N]" -n 1
echo # (optional) move to a new line
case "$REPLY" in 
  y|Y ) echo "Operation continues";;
  * ) echo "Operation is cancelled";;
esac