how to prompt for input from user in a linux shell script

When writing shell scripts, you will have scenarios you want to get input from the command line. And you would want to perform some specific action based on the user input. Programming a shell script to accept user input from the console is quite simple. We will see some examples of how you can do this in a bash script.

There are two different ways to prompt the user and read the input. One is the read command, while the other is to use the select command. Both of the commands achieve the same functionality, with some slight differences in usage and display.

using read command

The read shell built-in is probably the most widely used. It is also probably the easiest to use. The generic syntax of the read command is

read [options] [name..]

The read supports several different options. The important one in this context is -p that allows you to display a string as prompt or informative text. The -s or silet option can be used to silence the input so that it is not printed or visible in the display.

read -p "Would you like delete the files and folders? [Y/n]: " delconf

In the above example, the text is displayed and the script waits for the user input. When an input is entered by the user, it is stored in the variable delconf. You can now test the variable delconf and do something with it. So, a standard example could like this

read -p "Would you like delete the files and folders? [Y/n]: " delconf 
if [ $delconf == 'Y' ]; then     
    echo "Deleting all files" 
fi

In spite of the above example being simple, it does have some shortcomings. For example, there is no way to ensure that the user enters only valid values. One way to achieve this is to use a while loop

#!/bin/bash
shouldloop=true;
while $shouldloop; do
read -p "Would you like delete the files and folders? [Y/n]: " delconf
shouldloop=false;
if [ $delconf == 'Y' ]; then
   echo "Deleting all files"
elif [ $delconf == 'n' ]; then
   echo "Not deleting files"
else
   echo "Enter a valid response Y or n";
   shouldloop=true;
fi
done
bash shell script read user input

You can do some variation of the above example to achieve the ability to ensure a valid input from the user or any desired functionality.

using select command

select is a shell construct that lets you create menus easily. You can use it to get input from the user as well. The syntax of the select command is a very similar to the for loop in bash.

select WORD [in list]; do set of commands; done

So, the select has some additional advantages over read. You do not have to sanitize the input to make sure that you have a valid input. There is also no need for a while loop as with the read command above, because the select loops automatically in case of invalid entries.

In order to achieve the functionality as the example in the read section above, we could do something like this

echo "Would you like delete the files and folders?:"
select delconf in "Yes" "No" "Cancel"
do
    echo "You selected :" $delconf
    case $delconf in
        Yes ) echo "Deleting all files"; break;;
        No ) echo "Not deleting files"; break;;
        Cancel ) exit;;
    esac
done
select command in shell script

This will generate a menu with all your options with an associated number, that the user can use to select a particular option. This is generally useful, when you have options that are too long to type in. You also get a user interface that is pretty easy for the user to understand.

With the select command, you can use a command or another variable as your option list as well. This means it is very easy to create dynamic menus.

For example, if you want to display all jpg files in a folder that you want to delete but give the user the option to select the file. You could do something like this

echo "You can delete only one file at a time."
PS3="Select the file from above list (quit to exit): "
options=( $(find /usr/share/sddm/themes -iname *.jpg) )
select jpgname in "${options[@]}" "quit"
do
   if [ $jpgname == "quit" ]; then
    exit; 
   fi
   echo "File $jpgname will be deleted."   
done
shell script select command menu

Whether you choose to use the read or the select command will depend very much on the type of input you want from the user, and also the display format that you want. If you have a set of options that you want the users to choose from, then the select command would be good. If you want the user to be able to enter random strings or if the set of values that the user can enter is too big, then using the read command is probably the way to go.