Here, the menu item will be created from the data_list that can be an array or other data source. Each menu item retrieves from the data_list and stores in a variable to create the menu. ‘Select’ command can be used with ‘case’ command also to create the menu. Different types of menu creation tasks are shown here using very simple examples.
Example-1: Creating a simple menu
Create a bash file named select1.sh with the following bash script. Here, ‘select’ command will retrieve each data from the list and print the data as a menu. The following script contains the brand names of mobile phones and after executing the script it will create a menu of mobile brands and ask the user to choose any brand. It will print the name of the selected brand. The user has to press Ctrl+c to terminate from the script.
# Define the menu list here
select brand in Samsung Sony iphone symphony Walton
echo "You have chosen $brand"
Run the script.
The following output will appear after running the script and selecting the menu item number 1 and 4.
Example-2: Select command with a case statement
How you can create a bash menu with case statement is shown in this example. Create a bash file named select2.sh with the following bash script. After running the script, the user will select any menu item and case statement will match the selected value with case value. Here multiple case values are used for matching with the selected menu item. If none of the case value matches with the selected menu item then “Invalid entry” will print.
echo "Which Operating System do you like?"
# Operating system names are used here as a data source
select os in Ubuntu LinuxMint Windows8 Windows7 WindowsXP
case $os in
# Two case values are declared here for matching
echo "I also use $os."
# Three case values are declared here for matching
"Windows8" | "Windows7" | "WindowsXP")
echo "Why don't you try Linux?"
# Matching with invalid data
echo "Invalid entry."
Run the script.
The different output will appear for different selection. Here, 1 is selected the first time that is Ubuntu. it matched with first case value print the message “I also use Ubuntu”. 4 is selected next time and the menu item is Windows7 but there is no case value with this name. So, this time it printed “Invalid entry” and terminate from the script.
Example-3: Creating nested bash menu
When any menu is created under the other’s menu then it is called a nested menu. The nested menu can be created in the base by using two or more select and case statements. Create a bash file named select3.sh with the following bash script. Here, two select and case statements are used to implement the nested menu. The parent menu contains 4 items and sub-menu contains 3 items here.
# Parent menu items declared here
select item in Mouse Printer Monitor HDD
# case statement to compare the first menu items
case $item in
echo "Which type of monitor you like?"
# Sub-menu items here declared here
select sub_item in Samsung HP Dell
# case statement for sun-menu items
case $sub_item in
echo "The price of samsung printer is $100"
echo "New HP 880 printer price printer is $450"
# return to parent menu
echo "No Dell printer is available now"
# return to the parent menu
echo "Buy our new Samsung Monitor at low cost"
break # return to current (main) menu
echo "Good quality HDD are available in the stock"
# return from the script
Run the script.
After running the script, the parent menu with 4 items displayed. Here, only the second menu item of the parent menu contains sub-menu. After selecting 2, the sub-menu appeared and when the user selected 3 then it displayed the matching message of menu item 3 and return back to the parent menu.
Example-4: Create a bash menu with an array
An array variable can store multiple data. So, you can use an array as a data list for creating a base menu. How you can use an array with a select statement to create a menu is shown in this example. Create a bash file named select4.sh with the following bash script. Bash subroutine is used in this script to create a menu from the array. An array with three elements is used and menu_from_array () is called to create the menu. The script will check the selected menu item number is between 1-3 or not. If the number is not within this range then it will instruct the user select it within this range, it will print select menu item with other text.
select item; do
# Check the selected menu item number
if [ 1 -le "$REPLY" ] && [ "$REPLY" -le $# ];
echo "The selected operating system is $item"
echo "Wrong selection: Select any number from 1-$#"
# Declare the array
linux=('Ubuntu' 'Linux Mint' 'CentOS')
# Call the subroutine to create the menu
Run the script.
Here, 2 is selected first that is within the range and printed the text. 6 is selected the second time that is out of range and suggest the user select a number within the range.
To understand this code we should learn a few tricks 😉
- Because the in list is not provided in the select positional parameters are used which are in this case the words in the array passed to the function
- $REPLY stores the value input by the user
- the logical IF AND conditional in the function is checking the reply is between 1 and the size of the POSITIONAL PARAMETER array
Bash menu creation task by using a select statement is explained in this tutorial as simple as possible. There are many simple and fancy ways to use SELECT in bash when creating menus and we hope these examples give you a starting point to create your own combinations.