-
selectis a Bash construct for generating interactive menus. -
Syntax looks like a
forloop:select ITEM in [LIST] do [COMMANDS] done
-
[LIST]can be:- A list of strings separated by spaces
- A range of numbers
- Command substitution output
- An array
-
The menu is displayed with numbered options.
-
User chooses by entering a number:
ITEM→ holds the chosen valueREPLY→ holds the number entered
-
A custom prompt is set with
PS3.
PS3="Enter a number: "
select character in Sheldon Leonard Penny Howard Raj
do
echo "Selected character: $character"
echo "Selected number: $REPLY"
done-
Displays:
1) Sheldon 2) Leonard 3) Penny 4) Howard 5) Raj Enter a number:
#!/bin/bash
PS3="Select the Operation: "
echo "Calculator"
select opr in addition substraction multiplication division quit; do
case $opr in
addition)
read -p "Enter a first number:" num1
read -p "Enter a second number:" num2
echo "$num1 + $num2 = $(( $num1 + $num2 ))"
;;
substraction)
read -p "Enter a first number: " num1
read -p "Enter a second number: " num2
echo "$num1 - $num2 = $(( $num1 - $num2 ))"
;;
multiplication)
read -p "Enter a first number: " num1
read -p "Enter a second number: " num2
echo "$num1 * $num2 = $(( $num1 * $num2 ))"
;;
division)
read -p "Enter a first number: " num1
read -p "Enter a second number: " num2
echo "$num1 / $num2 = $(( $num1 / $num2 ))"
;;
quit)
break
;;
*)
echo "Invalid option $REPLY"
;;
esac
done
calculate () {
read -p "Enter the first number: " n1
read -p "Enter the second number: " n2
echo "$n1 $1 $n2 = " $(bc -l <<< "$n1$1$n2")
echo "$n1 $1 $n2 = $(expr $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#!/bin/bash
PS3="please select os? " # Custom prompt for select
select os in linux windows mac
do
case ${os} in
linux)
echo "you selected linux"
echo "thanks."
break
;;
windows)
echo "you selected windows"
echo "thanks."
break
;;
mac)
echo "you selected mac"
echo "thanks."
break
;;
*)
echo "Invalid"
;;
esac
done-
PS3→ sets the prompt shown whenselectasks for input. -
select os in ...→ shows a numbered menu:1) linux 2) windows 3) mac please select os? -
User enters a number (e.g.,
1) → the variable$osis set (linux). -
casestatement handles each possible choice. -
*case handles invalid/empty input. -
break→ exits the loop after a valid selection.
1) linux
2) windows
3) mac
please select os? 1
you selected linux
thanks.
please select os? 5
Invalid
To avoid looping forever on invalid input, add an empty check:
#!/bin/bash
PS3="please select os? "
select os in linux windows mac
do
if [[ -z "$os" ]]; then
echo "Invalid option"
continue
fi
case $os in
linux|windows|mac)
echo "you selected $os"
echo "thanks."
break
;;
esac
doneNow:
- If input is invalid (
5,10, blank), it printsInvalid optionand asks again. - If input is valid, it executes and exits.
💡 Pro Tip: You can also use select for menus like:
- File selection
- Service management
- Script option pickers