How to prompt for Yes/No/Cancel input in a Linux shell script
Last Updated :
14 Nov, 2022
You may have noticed that shell programs occasionally ask the user for confirmation by prompting [Y/n] or [Yes/No]. Knowing whether a user wishes to continue with the following stages or not is useful. A similar feature can be added to your script as well. This article will assist you with examples of this sort of input (Bash Script – Prompt to Confirm (Y/N, YES/NO)). You may have noticed that shell programs occasionally ask the user for confirmation by prompting [Y/n] or [Yes/No]. Knowing whether a user wishes to continue with the following stages or not is useful. A similar feature can be added to your script as well.
In this article, we will discuss how to prompt for user input for the following choices in a Linux shell script:
This task is fairly easy to create using a shell script and we shall how the same could be done for various situations. To prompt the input, we will use the read function of shell scripting and then use the case statements for performing those various choices.
We can simply get user input from the read command in BASH. It provides a lot of options and arguments it for more flexible usage, but we’ll cover them in the next few sections. For now, let’s see how a basic read command can be used.
Syntax of read function:
read -[option] variable
There are numerous options that can be supplied to the read command, allowing for customizable user input collection. Here, some of the few reasons are covered:
- Prompt String (-p)
- Password Input (-s)
- Changing the Delimiter (IFS)
- Parsing to the array (-a)
- Limiting Length of the Input (-n)
- Timed Input (-t)
Syntax of the case statements:
case "choice" in
option 1) <statements
.
.
*) default case
esac
Now let us see the usage of these in implementation for taking user choices in various examples.
Example 1: Install a Software
Let us create a script where we ask the user to install software, with the choices “yes”, “no”, and “cancel”.
Script:
#!/bin/bash
# prompting for choice
read -p "Do you want to install bc calculator. (y)Yes/(n)No/(c)Cancel:- " choice
# giving choices there tasks using
case $choice in
[yY]* ) echo "Making dummy install" ;;
[nN]* ) echo "Thanks for using the script" ;;
[cC]* ) echo "Installation cancelled" ;;
*) exit ;;
esac
This script will accept both Upper case and Lower-case initials for the three choices. The output is:
Output:
After executing the script, the user is prompted to enter his/her choice with 3 different options:- (y)Yes/(n)No/(c)Cancel. if the user enters (y) then the software starts its installation process, if the user enters (no) then the script is terminated and if the user enters (cancel) then the installation process is been terminated or canceled.
Example 2: Using if-else block
Let us take choices and execute them with the if-else block.
Script:
#!/bin/bash
read -p "Enter your choice. Yes(y) / No(n) / Cancel(c):- " choice
if [ "$choice" = "y" ]; then
echo "Making the dummy install!"
elif [ "$choice" = "n" ];then
echo "Making no install"
elif [ "$choice" = "c" ];then
echo "Installation cancelled"
else
echo "Wrong!"
fi
Here we use the string comparison using the ‘=’ operator for matching the input choices.
Output:
Here, instead of using switch case blocks, we have used if-else statements for performing actions on software installation through the input given by the user.

Similar Reads
How to prompt for Yes/No/Cancel input in a Linux shell script
You may have noticed that shell programs occasionally ask the user for confirmation by prompting [Y/n] or [Yes/No]. Knowing whether a user wishes to continue with the following stages or not is useful. A similar feature can be added to your script as well. This article will assist you with examples
3 min read
How to Create a Shell Script in linux
Shell is an interface of the operating system. It accepts commands from users and interprets them to the operating system. If you want to run a bunch of commands together, you can do so by creating a shell script. Shell scripts are very useful if you need to do a task routinely, like taking a backup
7 min read
How to Run a Shell Script in Linux
Shell scripts are a powerful way to automate tasks and manage system processes in Linux. These scripts, written in shell programming languages like Bash, allow users to execute a sequence of commands efficiently. In this guide, we'll show the steps to check a shell script in Linux before running it,
4 min read
How to display nc return value in Linux shell script?
When you need to monitor network connections and understand their status, creating a custom bash script can be an invaluable solution. This article demonstrates how to build a bash script that checks network connections using the nc command and displays the corresponding exit status. By providing a
6 min read
Bash shell script to find if a number is perfect or not
In this article, we will discuss how to write a bash script to find if a number is perfect or not. A perfect number is defined as, a positive number that is equal to the sum of its proper divisors. Smallest no is 6 ex= 1,2,3 are divisor of 6 and 1+2+3=6 Method 1: Using While LoopRead the input using
3 min read
How to Enable Shell Script Debugging Mode in Linux?
Linux is a very popular operating system especially among developers and in the computer science world, as it comes along with its powerful ability to play with the kernel. Major Linux distributions (often referred to as Linux distros) are Open Source (Source Code available and can be modified on re
5 min read
How to check any script is running in linux using Python?
Python is a strong and exponentially growing programming language in the present day. There is a multiple-way to check which script is running in the background of a Linux environment. One of them is using the subprocess module in python. Subprocess is used to run new programs through Python code by
2 min read
Shell Script To Check For a Valid Floating-Point Value
User Input can be hectic to manage especially if it's about numbers. Let's say you wanted to perform mathematical calculations in your app or script. It's not hard to say that it will have to deal with floating-point numbers. It's easy in many statically typed programming languages but if it has to
4 min read
How to Execute Shell Commands in a Remote Machine in Python?
Running shell commands on a Remote machine is nothing but executing shell commands on another machine and as another user across a computer network. There will be a master machine from which command can be sent and one or more slave machines that execute the received commands. Getting Started We wil
3 min read
Shell Scripting - How do we can create a sample ~/.dialogrc file
The dialog command allows you to display a variety of questions or messages from a shell script using dialog boxes. The ~/.dialogrc file is a run-time configuration file for dialog commands. Basically, it is the default configuration file. Also, the ~/.dialogrc file allows us to customize various as
3 min read