Bash Select (Make Menus)

Introduction

Before we start discussing about bash select, let's first understand-What is Select?

In Bash, the select construct is used to create menus and interactive selection prompts in scripts. It allows users to choose options from a list displayed in a menu format. By utilizing the select statement, you can create interactive scripts with user-friendly interfaces that enhance user experience and simplify program interaction.

In this tutorial, we'll go over the fundamentals of Bash's select construct.

You can create menus using the select construct.

Bash select Construct

From a list of items, the select construct creates a menu. It's almost identical to the for loop in terms of syntax:

select ITEM in [LIST]
do
  [COMMANDS]
done

A series of strings separated by spaces, a range of numbers, the output of a command, an array, and so on can all be used in the [LIST]. The PS3 environment variable can be used to set a custom prompt for the select construct.

When the select construct is used, each item in the list is printed (standard error) on the screen, accompanied by a number.

The value of [ITEM] is set to that item if the user provides a number that corresponds to the number of one of the presented items. REPLY is a variable that stores the value of the selected item. If the user input is blank, the prompt and menu list is displayed once more.

Until the break instruction is issued, the select loop will continue to operate and prompt human input.

Take a look at the following simple example to see how the chosen construct works.

PS3="Enter a number: "

select character in Sheldon Leonard Penny Howard Raj
do
    echo "Selected character: $character"
    echo "Selected number: $REPLY"
done

The script will display a menu with a list of options and a number, as well as the PS3 prompt. The script will print the specified character and number when the user enters a number:

1) Mike
2) Samantha
3) Jane
4) Howard
5) Raju
Enter a number: 3
Selected character: Jane
Selected number: 3
Enter a number:

Bash select Example

Select is commonly used in conjunction with case of if statements.

Let's look at a more real-world case. It's a straightforward calculator that asks for input and does fundamental arithmetic operations like addition, subtraction, multiplication, and division.

PS3="Select the operation: "

select opt in add subtract multiply divide quit; do

  case $opt in
    add)
      read -p "Enter the first number: " n1
      read -p "Enter the second number: " n2
      echo "$n1 + $n2 = $(($n1+$n2))"
      ;;
    subtract)
      read -p "Enter the first number: " n1
      read -p "Enter the second number: " n2
      echo "$n1 - $n2 = $(($n1-$n2))"
      ;;
    multiply)
      read -p "Enter the first number: " n1
      read -p "Enter the second number: " n2
      echo "$n1 * $n2 = $(($n1*$n2))"
      ;;
    divide)
      read -p "Enter the first number: " n1
      read -p "Enter the second number: " n2
      echo "$n1 / $n2 = $(($n1/$n2))"
      ;;
    quit)
      break
      ;;
    *) 
      echo "Invalid option $REPLY"
      ;;
  esac
done

The menu and PS3 prompt are displayed when the script is run. The user must first choose an operation and then enter two numbers. The script will output the outcome based on the user's input. After each option, the user will be requested to conduct a new procedure until the break instruction is triggered.

Output

1) add
2) subtract
3) multiply
4) divide
5) quit
Select the operation: 1
Enter the first number: 4
Enter the second number: 5
4 + 5 = 9
Select the operation: 2
Enter the first number: 4
Enter the second number: 5
4 - 5 = -1
Select the operation: 9
Invalid option 9
Select the operation: 5

This script has one disadvantage that it can only deal with integers.

To execute mathematical computations, we use the bc tool, which supports floating numbers. Additionally, the repeated code is contained within a function.

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

PS3="Select the operation: "

select opt in add subtract multiply divide quit; do

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

1) add
2) subtract
3) multiply
4) divide
5) quit
Select the operation: 4
Enter the first number: 8
Enter the second number: 9
8 / 9 =  .88888888888888888888
Select the operation: 5 

FAQs on Bash Select (Make Menus)

How does the select statement work?

The select statement displays a numbered list of options and prompts the user for input. Once the user selects an option, the corresponding code block associated with that option is executed.

How do I use the select statement to create a menu?

To create a menu using select, you define a variable to store the user's selection and use the select statement followed by a list of options. You then provide a code block within do and done keywords to execute the selected option's corresponding actions.

Can I customize the prompt displayed by select?

Yes, you can customize the prompt displayed by setting the PS3 (Prompt String 3) variable. By default, PS3 is set to #?, but you can change it to suit your preference.

How can I display a list of options using select?

Inside the select statement, you provide a list of options as separate values. Each value represents an individual choice presented to the user in the menu.

Can I include default options in the select menu?

Yes, you can include a default option labeled "Other" or "Exit" as the last option in the list. This allows users to choose an option other than those presented in the menu.

How do I capture the user's selection in a select menu?

The user's selection is captured in a variable defined before the select statement, typically named REPLY (although you can use any variable name). The selected value is stored in REPLY for further processing within the script.

Is it possible to nest select statements?

Yes, you can nest select statements to create sub-menus within your script. This allows users to navigate through multiple levels of menu options.

Conclusion

The select construct makes it simple to create menus. It comes in handy when building shell scripts that need user input.

If you have any queries, please leave a comment below and we’ll be happy to respond to them.