“Yes/No” in Bash Script – Prompt for Confirmation

Very often in bash scrips you need to ask for user input that requires a Yes or No answer.

For example, you may want to put a quick “Are you sure?” prompt for confirmation before execution of some potentially dangerous part of a bash script.

In this article you’ll find three easiest and fastest ways to prompt for “Yes/No” confirmation in bash script.

Prompt To Continue In Bash

The best way to prompt for a confirmation to continue in a bash script is to use the read command (source):

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

Simple “Yes/No” Dialog In Bash

One of the most widely used method to ask user for a confirmation in a bash script is to combine read + case commands (source):

while true; do
    read -p "Do you wish to install this program?" yn
    case $yn in
        [Yy]* ) make install; break;;
        [Nn]* ) exit;;
        * ) echo "Please answer yes or no.";;

Select From “Yes/No” Menu In Bash

Another easy method is to use select command (source):

echo "Do you wish to install this program?"
select yn in "Yes" "No"
case $yn in
    Yes ) make install;;
    No ) exit;;
5 (1)
Article Rating (1 Votes)
Rate this article
There are no attachments for this article.
Comments (1)
Comment By cellcore - Mon, Nov 30th, 2020 9:24 PM
awesome and straight to the point
Full Name
Email Address
Security Code Security Code
Related Articles RSS Feed
Time conversion using Bash
Viewed 1313 times since Fri, May 25, 2018
O’Reilly’s CD bookshelf
Viewed 7586 times since Wed, Jun 27, 2018
HowTo: Clear BASH History
Viewed 1021 times since Mon, Feb 18, 2019
awk printf
Viewed 11454 times since Wed, Aug 19, 2020
Bash: Read File Line By Line – While Read Line Loop
Viewed 1136 times since Mon, Feb 18, 2019
Bash: String Length – How To Find Out
Viewed 1171 times since Mon, Feb 18, 2019
Linux: Repeat Command N Times – Bash FOR Loop
Viewed 1538 times since Mon, Feb 18, 2019
How to get a password from a shell script without echoing - solutions
Viewed 9693 times since Fri, Feb 22, 2019
Linux / UNIX: Convert Epoch Seconds To the Current Time
Viewed 1794 times since Fri, May 25, 2018
30 Handy Bash Shell Aliases For Linux / Unix / MacOS
Viewed 1180 times since Thu, Feb 11, 2021