π§ Bash: Select Menus β Build Interactive Scripts with select Loop
π§² Introduction to Bash select β Create Interactive Menus Easily
The select command in Bash is a powerful but underused feature that lets you create interactive command-line menus with minimal code. It’s perfect for prompting users to pick from a list of optionsβideal for scripts involving file choices, configuration menus, automation tasks, or even setup wizards.
Unlike read, which requires manual validation, select handles user input and numbering automatically, making it one of the simplest ways to gather structured choices in Bash scripts.
π― In this article, youβll learn:
- How the Bash
selectloop works - Syntax and best practices for menu creation
- Real-world use cases with examples
- How to handle invalid input and loop menus
π§Ύ Basic Syntax of select
select variable in list; do
commands
done
| Part | Description |
|---|---|
variable | The chosen item from the list |
list | Menu options presented to the user |
do block | Executes after user makes a selection |
π‘ The user’s choice is stored in
$REPLYand matched to the selected item.
π§ͺ Example 1: Simple Select Menu
#!/bin/bash
select fruit in Apple Banana Orange Exit; do
case $fruit in
Apple) echo "You chose Apple";;
Banana) echo "You chose Banana";;
Orange) echo "You chose Orange";;
Exit) break;;
*) echo "Invalid option";;
esac
done
β Output:
1) Apple
2) Banana
3) Orange
4) Exit
#? 2
You chose Banana
π§ͺ Example 2: Loop Until Exit Selected
#!/bin/bash
echo "Select a file action:"
PS3="Enter your choice: "
select action in Create Delete Rename Quit; do
case $action in
Create)
echo "Creating a file..."
;;
Delete)
echo "Deleting a file..."
;;
Rename)
echo "Renaming a file..."
;;
Quit)
echo "Exiting."
break
;;
*)
echo "Invalid selection."
;;
esac
done
β Output:
1) Create
2) Delete
3) Rename
4) Quit
Enter your choice:
βοΈ Customizing the Prompt with PS3
By default, the prompt is #? . You can change it by setting PS3 before select:
PS3="Choose an option (1-4): "
π§
PS3controls the text shown when prompting for user input.
π οΈ Accessing Raw User Input with $REPLY
Even if a user enters a number that doesnβt match an option, you can access it using $REPLY.
select opt in Start Stop Restart; do
echo "You selected '$opt' (input number: $REPLY)"
done
π§ Best Practices for select Menus
| Practice | Why It Helps |
|---|---|
| Add an βExitβ or βQuitβ option | Allows graceful script termination |
Handle *) case | Catches invalid or blank input |
Use break to exit loops | Prevents infinite user prompting |
Set a descriptive PS3 prompt | Improves user experience |
π Summary: Bash Select Menus
The select loop is a clean, interactive way to prompt users in Bash. It requires little code, provides built-in numbering, and enhances usability for scripts with choices.
π Key Takeaways:
- Use
selectto create interactive numbered menus - Use
$REPLYto access raw user input - Combine with
caseto build powerful CLI tools - Set
PS3to customize prompts
βοΈ Real-world Uses:
- Scripted installers or setup menus
- File management utilities
- Configuration scripts for services
- User-interactive backup or sync tools
β FAQ β Bash Select Menus
β What is the default prompt in select?
β
It is #? . You can change it using:
PS3="Choose your option: "
β How do I break out of a select loop?
β
Use the break command in a menu case like:
Exit) break ;;
β What if the user enters an invalid number?
β
Handle it with:
*) echo "Invalid option" ;;
β Can I use select without case?
β
Yes, but using case makes branching and validation easier.
β How do I get the number the user typed?
β
Use the special variable $REPLY.
Share Now :
