13. Bash Select
Overview¶
The Bash select statement is used to create a simple interactive selection menu in the terminal. It automatically displays numbered options and waits for the user to choose one.
It is useful when a Bash script needs the user to select one option from a predefined list.
Basic Syntax¶
When the select statement runs, Bash:
-
Displays each option with a number.
-
Displays the value of
PS3as the prompt. -
Waits for the user to enter a number.
-
Stores the selected option in the specified variable.
-
Executes the commands inside the
do ... doneblock. -
Continues displaying the menu until the loop is terminated.
Example¶
#!/bin/bash
PS3='Choose one word: '
select word in "linux" "bash" "scripting" "tutorial"
do
echo "The word you have selected is: $word"
# Break the loop after one selection
break
done
exit 0
Output¶
$ ./select.sh
1) linux
2) bash
3) scripting
4) tutorial
Choose one word: 2
The word you have selected is: bash
Understanding the Script¶
Shebang¶
Specifies that the script should be executed using Bash.
PS3¶
PS3 defines the prompt displayed by the select statement.
Without setting PS3, Bash uses the default prompt:
Using a custom prompt makes the menu easier for the user to understand.
select¶
This creates a menu containing four options.
Bash automatically assigns numbers to the options:
The selected value is stored in the variable word.
For example, if the user enters:
then:
do ... done¶
The commands between do and done are executed after the user makes a selection.
break¶
A select statement normally continues running and displays the menu again after each iteration.
break terminates the loop after the first selection.
Without break, the menu would continue to appear:
Using select With case¶
select is commonly combined with case when different actions need to be performed for different selections.
#!/bin/bash
PS3="Select an option: "
select option in "Show Date" "Show Current Directory" "Exit"
do
case $option in
"Show Date")
date
;;
"Show Current Directory")
pwd
;;
"Exit")
echo "Exiting..."
break
;;
*)
echo "Invalid selection"
;;
esac
done
Example Output¶
Selecting option 2 executes:
Selecting option 3 executes:
and terminates the menu.
Handling Invalid Input¶
If the user enters a number that does not correspond to an option, the selection variable is empty.
For example:
A case statement can be used to handle this:
#!/bin/bash
PS3="Choose an option: "
select option in "Linux" "Bash" "Exit"
do
case $option in
"Linux")
echo "You selected Linux"
;;
"Bash")
echo "You selected Bash"
;;
"Exit")
break
;;
*)
echo "Invalid selection"
;;
esac
done
Important Variables¶
The select statement works with several useful Bash variables.
REPLY¶
REPLY contains the number entered by the user.
#!/bin/bash
PS3="Choose an option: "
select option in "Linux" "Bash" "Exit"
do
echo "Selected number: $REPLY"
echo "Selected option: $option"
break
done
If the user selects option 2:
This is useful when you need both the selected option and its menu number.
Practical Example¶
A simple system administration menu can be created using select:
#!/bin/bash
PS3="Select an action: "
select action in "Show System Information" "Show Disk Usage" "Show Memory Usage" "Exit"
do
case $REPLY in
1)
uname -a
;;
2)
df -h
;;
3)
free -h
;;
4)
echo "Exiting..."
break
;;
*)
echo "Invalid selection"
;;
esac
done
This creates an interactive menu where the user can choose different system administration commands.
Key Points¶
-
selectcreates interactive numbered menus in Bash. -
PS3controls the selection prompt. -
The selected option is stored in the specified variable.
-
$REPLYcontains the number entered by the user. -
casecan be combined withselectto perform different actions. -
breakcan terminate the selection loop. -
Without
breakor another loop-control mechanism, the menu continues to appear.