Interactive Bash scripts will often include a yes or no prompt in order to ask for user verification before proceeding with a set of instructions or canceling the procedure.
If a user answers yes
to the prompt, the Bash script will typically proceed with its task, and if a user answers no
, the script will either exit or move on to a different part of the script.
In this tutorial, you will see how to create a yes/no prompt in a Bash script on a Linux system. See some of our examples below to learn how a yes/no prompt works.
In this tutorial you will learn:
- How to create a yes or no prompt in Bash
- How to loop a yes or no prompt for invalid responses
- How to check for lowercase or uppercase responses

Category | Requirements, Conventions or Software Version Used |
---|---|
System | Any Linux distro |
Software | Bash shell (installed by default) |
Other | Privileged access to your Linux system as root or via the sudo command. |
Conventions |
# – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command$ – requires given linux commands to be executed as a regular non-privileged user |
Bash script: YES/NO prompt example
- We will read input from the command line by using the
read
command in our Bash script. This is best coupled with a case statement that can check to see if the user enteredyes
orno
or something else.#!/bin/bash read -p "Do you want to proceed? (yes/no) " yn case $yn in yes ) echo ok, we will proceed;; no ) echo exiting...; exit;; * ) echo invalid response; exit 1;; esac echo doing stuff...
In this script, the
read
command asks the user if they would like to proceed, and waits for input. The response from the user is stored in variable$yn
. Thecase
statement then determines if the user enteredyes
,no
, or something else, and proceeds accordingly. Here are the results when we execute the script:$ ./test.sh Do you want to proceed? (yes/no) yes ok, we will proceed doing stuff... $ ./test.sh Do you want to proceed? (yes/no) no exiting... $ ./test.sh Do you want to proceed? (yes/no) fdsfdsf invalid response
- One problem with the script above is that if a user erroneously enters an invalid response, the script does not attempt to get a correct answer, and instead proceeds to exit. This is not a problem in some situations, but sometimes we may want to continually prompt the user for a valid yes or no response. In that case, we can simply wrap our yes/no prompt in a
while
loop.#!/bin/bash while true; do read -p "Do you want to proceed? (yes/no) " yn case $yn in yes ) echo ok, we will proceed; break;; no ) echo exiting...; exit;; * ) echo invalid response;; esac done echo doing stuff...
Note that it was also necessary to add a
break
to theyes
clause of ourcase
statement. This tells Bash to exit thewhile
loop in case the user answer affirmatively. We also dropped theexit
command from our invalid response clause. Here is what happens when we enter invalid responses now:$ ./test.sh Do you want to proceed? (yes/no) aaa invalid response Do you want to proceed? (yes/no) bbb invalid response Do you want to proceed? (yes/no) yes ok, we will proceed doing stuff...
- Rather than forcing our users to type out a full
yes
orno
, it is much more conventional to allow a response ofy
orn
on Linux systems. Furthermore, let’s make sure our response is not case sensitive, so either a capital or lowercase letter can be entered.
#!/bin/bash while true; do read -p "Do you want to proceed? (y/n) " yn case $yn in [yY] ) echo ok, we will proceed; break;; [nN] ) echo exiting...; exit;; * ) echo invalid response;; esac done echo doing stuff...
View the result below. This is much more convenient for the user since they do not need to spell the word, and do not need to worry about if their caps lock is on or not.
$ ./test.sh Do you want to proceed? (y/n) Y ok, we will proceed doing stuff... $ ./test.sh Do you want to proceed? (y/n) N exiting... $ ./test.sh Do you want to proceed? (y/n) y ok, we will proceed doing stuff... $ ./test.sh Do you want to proceed? (y/n) n exiting...
Closing Thoughts
In this tutorial, we saw how to create a simple yes or no prompt in a Bash script on a Linux system. There are more ways this can be done, but the most common is a read
command and a case
statement inside of a while
loop. It is also most common to ask for a one letter response rather than typing out a whole word, although you could easily adapt your script to accept either kind of answer.