Using the select
construct you can generate menus. In this guide, we will discuss about the basics of the select construct in Bash.
Bash select Construct
You can generate a menu from the list of items using the select
construct. Following is the syntax for the select
construct and it is similar to the for loop syntax:
select ITEM in [LIST]
do
[COMMANDS]
done
Here, in [LIST]
you can pass a range of numbers, an array, series of strings separated by space, output of a command, etc. Using the PS3
environment variable you can set a custom prompt for the select
construct.
Each item from the list will be printed on the screen when the select construct is invoked. If the entered number will matched with the number of the displayed items then it will set the value to that item. The variable REPLY
holds the value of the selected item. If the user input is empty, the prompt and the menu list are displayed again.
The select
loop will continue to run and prompt for user input until the break command is executed.
For better understanding let’s see an example of select
construct:
PS3="Input a number: "
select state in Florida California Texas Alaska
do
echo "Selected state: $state"
echo "Selected number: $REPLY"
done
This script will show a menu with the list of items with the accompanying number and the PS3
prompt. Once the user enter any number the selected state and the number will be print on screen.
1) Florida
2) California
3) Texas
4) Alaska
Enter a number: 2
Selected character: California
Selected number: 2
Enter a number:
Bash select Example
Generally, the select
is used with the case or if statements. Below is given simple example for better understanding.
It will do simple arithmetic operations on basis of user inputs.
PS3="Select the arithmetic operation: "
select opt in addition subtraction multiplication division quit; do
case $opt in
addition)
read -p "First number: " n1
read -p "Second number: " n2
echo "$n1 + $n2 = $(($n1+$n2))"
;;
subtraction)
read -p "First number: " n1
read -p "Second number: " n2
echo "$n1 - $n2 = $(($n1-$n2))"
;;
multiplication)
read -p "First number: " n1
read -p "Second number: " n2
echo "$n1 * $n2 = $(($n1*$n2))"
;;
division)
read -p "First number: " n1
read -p "Second number: " n2
echo "$n1 / $n2 = $(($n1/$n2))"
;;
quit)
break
;;
*)
echo "Invalid option $REPLY"
;;
esac
done
In output it will show the menu and PS3
prompt for user input. User have to select the operation and enter two numbers for perform operation. On the base of user selection the script will echo the result. It will also ask for the new operation after each selection until the break
command is executed.
1) addition
2) subtraction
3) multiplication
4) division
5) quit
Select the arithmetic operation: 1
First number: 2
Second number: 4
2 + 4 = 6
Select the operation: 3
First number: 4
Second number: 5
4 * 5 = 20
Select the operation: 7
Invalid option 7
Select the operation:
The disadvantage of this script is that it will work only with the integers.
We can also make the above example with more logic and reducing of codes by making function. We will use the bc
tools which supports the floating numbers.
calculation () {
read -p "First number: " n1
read -p "Second number: " n2
echo "$n1 $1 $n2 = " $(bc -l <<< "$n1$1$n2")
}
PS3="Select the arithmetic operation: "
select opt in addition subtraction multiplication division quit; do
case $opt in
addition)
calculate "+";;
subtraction)
calculate "-";;
multiplication)
calculate "*";;
division)
calculate "/";;
quit)
break;;
*)
echo "Invalid option $REPLY";;
esac
done
1) addition
2) subtraction
3) multiplication
4) division
5) quit
Select the arithmetic operation: 4
First number: 3
Second number: 7
3 / 7 = 0.428571428571429
Select the operation: 5
Conclusion
You learned how to generate menus using the select construct. It useful when require user input in shell scripts.
If you have any questions or feedback, feel free to leave a comment.