Bash Select (Make Menus) | Explained

The “bash select construct” is a feature of the Bash shell, which is a command-line interpreter for Linux operating systems. It is used to present a list of options to the user and allow them to choose one or more of those options by entering a number or letter associated with the option.

This can help create interactive command-line programs or scripts or for creating menus or user interfaces in a shell script.

This article teaches the use of bash select in Linux Shell and will cover the following aspects.

The detailed explanation of the above is as follows.

How to Make Menus Using Bash?

If you’ve done programming or coding in almost every programming language, then you’d see the ‘for loop’ and bash select construct is almost of the same syntax as seen below.

select ITEM in [List]
do
     [specify the executable commands]
done

In the above syntax of bash select construct, the ‘List’ can have a sequence of strings to be separated by blank spaces, integers (range), an array, or the output of a command. The ‘ITEM’ corresponds to the item selected in the ‘LIST’ and is set to that. In the body of ‘do,’ you’d need to specify the commands you want to execute when the user makes the selection.

How to use it? You’ll get to know this in the examples below.

Example 1: Creating a Simple Menu

In this example, we’ve created a simple menu with four numbers containing the names of the animals in each. Once the user selects a number, the terminal will show what animal at which number is selected.

#!/bin/bash
PS3="Enter a number: "

select animal in Lion Dog Cheetah Elephant
do
    echo "You Selected the Animal: $animal"
    echo "You Selected the Number: $REPLY"
done

In the above script, we’ve used the ‘PS3’, an environment variable used as a custom prompt for Select Construct. Once the user enters any number from the given list, it is stored inside the ‘REPLAY’ variable.

Now let’s execute this script (named ‘simple.sh’) using this command to see the results.

$ sudo bash simple.sh

As you can see, all the ITEMS are automatically arranged in a list, and to test its functionality, we entered ‘2’, which shows that we selected the animal Dog.

Example 2: Creating a Menu with the ‘or’ Statement

To add more complexity, we’re adding the ‘or’ statement in our script, which follows.

#!/bin/bash 

echo "Which Operating System do you like?"

select os in Ubuntu Fedora centOS LinuxMint
do
case $os in 
  "Ubuntu"|"LinuxMint")
     echo "This is the most popular Linux distro $os."
     ;;
  "Fedora"|"centOS")
     echo "It is a good Linux distro"
   ;;
*)
echo "Invalid entry."
break
;;
esac
done

In the above script, we’ve asked the user about the operating system they are using, which is stored the variable ‘os’ and then displays the output text according to the choice made by the user. If the user enters an invalid number, for example, ‘ 5’, then the script will stop, as seen in the below script named ‘os.sh’.

$ sudo bash os.sh

You can replace the name ‘os.sh’ with the name of your script.

Example 3: Creating a Calculator

In the 3rd example, we’ve created a script for a calculator that takes two values and a sign (+,-,*, or /) as input and then performs calculations based on them. Here’s the script.

calculator () {
  read -p "Enter the first number: " n1
  read -p "Enter the second number: " n2
  echo "$n1 $1 $n2 = " $(bc -l <<< "$n1$1$n2")
}

PS3="Please Select an operation to perform: "

select option in add subtract multiply divide quit; do

  case $option in
    add)
      calculator "+";;
    subtract)
      calculator"-";;
    multiply)
      calculator"*";;
    divide)
      calculator "/";;
    quit)
      break;;
    *) 
      echo "Invalid option $REPLY";;
  esac
  done

We’ve created a function to avoid repetitions that take the input and perform the calculations. In the echo line, you’d notice that we’ve used ‘bc’ to tackle floating numbers.

Here’s how we’d execute this script we named ‘test.sh’.

$ sudo bash test.sh

In the above script, we first entered the number 4 (to divide), and then we can see the results after entering the two numbers. The same is the case with the add operator but with different numbers, of course, and finally, we quit the program by entering ‘5’.

Following the above examples, we hope you understand the science behind the Bash Select.

Conclusion

The shell scripts that require user input, such as the calculator we’ve created above, require the use of Bash Select Construct, which also gives the ease of navigation through the menus in Linux Shell. This guide elaborates on how to create menus using the bash select.