Bash Command Line Confirmation Prompt

Creating Yes/No [y/n] Confirmation Prompt in Bash Command Line

Implementing a yes/no [y/n] confirmation prompt in Bash command line. Make the program exit when no or n is selected. Here's an example. <pre><code>read -p "Are you sure? " -n 1 -recho&nbsp;&nbsp;# (optional) move to a new lineif [[ ! $REPLY =~ ^[Yy]$ ]]then...

Shou Arisaka
1 min read
Oct 2, 2025

Implementing a yes/no [y/n] confirmation prompt in Bash command line. Make the program exit when no or n is selected.

Hereโ€™s an example.

read -p "Are you sure? " -n 1 -r
echo    # (optional) move to a new line
if [[ ! $REPLY =~ ^[Yy]$ ]]
then
[[ "$0" = "$BASH_SOURCE" ]] && exit 1 || return 1
fi

echo "Yes"

Share this article

Shou Arisaka Oct 2, 2025

๐Ÿ”— Copy Links