BASH Programming

Bash Select Command

Select‘ command is a very useful bash command for bash menu creation. Different types of menu generation task, creating menu based director list, creating a menu from file content etc. can be done using bash select command. How you can implement these types of tasks using bash select command are shown in this tutorial.

Syntax:

select v in data_list
do
statement1
Statement2
Statement3
done

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.

#!/bin/bash
# Define the menu list here
select brand in Samsung Sony iphone symphony Walton
do
echo "You have chosen $brand"
done

Output:

Run the script.

$ bash select1.sh

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.

#!/bin/bash
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
do

case $os in
# Two case values are declared here for matching
"Ubuntu"|"LinuxMint")
echo "I also use $os."
;;
# Three case values are declared here for matching
"Windows8" | "Windows10" | "WindowsXP")
echo "Why don't you try Linux?"
;;
# Matching with invalid data
*)
echo "Invalid entry."
break
;;
esac
done

Output:
Run the script.

$ bash select2.sh

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.

#!/bin/bash
while true
do
# Parent menu items declared here
select item in Mouse Printer Monitor HDD
do

# case statement to compare the first menu items
case $item in
Mouse)
echo "Which type of monitor you like?"
;;
Printer)
# Sub-menu items here declared here
select sub_item in Samsung HP Dell
do
# case statement for sun-menu items
case $sub_item in
Samsung)
echo "The price of samsung printer is $100"
break
;;
HP)
echo "New HP 880 printer price printer is $450"
# return to parent menu
break
;;
Dell)
echo "No Dell printer is available now"
# return to the parent menu
break 2
;;
esac
done
break
;;
Monitor)
echo "Buy our new Samsung Monitor at low cost"
break # return to current (main) menu
;;
HDD)
echo "Good quality HDD are available in the stock"
# return from the script
break 2
esac
done
done

Output:

Run the script.

$ bash select3.sh

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.

menu_from_array ()
{

select item; do
# Check the selected menu item number
if [ 1 -le "$REPLY" ] && [ "$REPLY" -le $# ];

then
echo "The selected operating system is $item"
break;
else
echo "Wrong selection: Select any number from 1-$#"
fi
done
}

# Declare the array
linux=('Ubuntu' 'Linux Mint' 'CentOS')

# Call the subroutine to create the menu
menu_from_array "${linux[@]}"

Output:

Run the script.

$ bash select4.sh

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.

Conclusion

Bash menu creation task by using a select statement is explained in this tutorial as simple as possible. Hope, the coder who are interested to work on bash menu and learn bash select statement will get help from this tutorial. Thank you.

About the author

Fahmida Yesmin

Fahmida Yesmin

I am a trainer of web programming courses. I like to write article or tutorial on various IT topics. I have a YouTube channel where many types of tutorials based on Ubuntu, Windows, Word, Excel, WordPress, Magento, Laravel etc. are published: Tutorials4u Help.