Create Interactive Menus in Bash
Summary:
Use select
and loops to build CLI menus.
Command-line menus can greatly enhance the usability and interactivity of Bash scripts. Whether you're building utilities or just want to provide friendly script options, interactive menus using Bash's select
and loops can help. In this post, we’ll explore how to create clear, intuitive CLI menus and guide users through choices step by step.
Why Menus in Bash?
Bash scripts often require user input. Rather than re-typing answers to prompts or remembering command options, menus provide an efficient, error-reducing interface. They’re especially useful for:
- System administration scripts
- Installation assistants
- Automation tools for teams
- Learning and demo environments
1. The select
Statement
Bash’s select
command is a built-in way to present numbered options. It's simple and powerful.
Syntax:
select VARIABLE in OPTIONS; do
# actions
done
VARIABLE
is set to the chosen option’s value.- The loop continues until you break it.
Example:
#!/bin/bash
echo "Choose your favorite fruit:"
select fruit in Apple Banana Orange Quit
do
case $fruit in
Apple|Banana|Orange)
echo "You selected: $fruit"
break
;;
Quit)
echo "Goodbye!"
break
;;
*)
echo "Invalid option."
;;
esac
done
How it works:
On running, Bash presents:
1) Apple
2) Banana
3) Orange
4) Quit
#?
When the user enters a number, $fruit
gets the corresponding value.
2. Custom Menus with Loops
select
is great, but sometimes you want finer control—for validation, non-numbered prompts, complicated workflows, or non-list menus.
Example: Menu Loop
#!/bin/bash
while true; do
echo "MAIN MENU:"
echo "1) Show disk usage"
echo "2) Show memory usage"
echo "3) Quit"
read -p "Enter your choice [1-3]: " choice
case $choice in
1) df -h ;;
2) free -m ;;
3) echo "Exiting." ; break ;;
*) echo "Invalid option." ;;
esac
echo ""
done
Features:
- Input is validated.
- Each option triggers a command.
- Menu loops until “Quit” is selected.
3. Making Menus User-Friendly
Here are a few tips for effective Bash menus:
a. Add Input Validation
Use patterns and conditions to catch errors and guide users.
if [[ ! $choice =~ ^[1-3]$ ]]; then
echo "Please enter a valid number (1, 2, or 3)."
fi
b. Clear the Screen
Make repeated menus cleaner:
clear
c. Use Functions for Actions
Structure actions as functions for clarity and reusability.
show_disk() {
df -h
}
show_mem() {
free -m
}
4. Combining select
and Loops
They’re not mutually exclusive! Sometimes you want selection and advanced handling:
while true; do
echo "Choose an action:"
select action in List_Files Show_Date Quit; do
case $action in
List_Files)
ls
break
;;
Show_Date)
date
break
;;
Quit)
exit 0
;;
*)
echo "Invalid option."
;;
esac
done
echo
done
5. A Practical Example: System Info Menu
Here’s a complete, ready-to-use system info menu script:
#!/bin/bash
while true; do
clear
echo "===== SYSTEM INFO MENU ====="
echo "1) Show disk usage"
echo "2) Show memory usage"
echo "3) Show logged-in users"
echo "4) Show running processes"
echo "5) Quit"
read -p "Choose an option [1-5]: " opt
case "$opt" in
1) echo; df -h ; read -p "Press Enter to continue...";;
2) echo; free -m ; read -p "Press Enter to continue...";;
3) echo; who ; read -p "Press Enter to continue...";;
4) echo; ps aux | less ;;
5) echo "Goodbye!"; break ;;
*) echo "Invalid option!"; sleep 1 ;;
esac
done
Conclusion
With Bash's select
command and flexible use of loops, you can build interactive CLI menus in minutes. Menus make scripts friendlier, reduce user error, and offer a professional touch. Try adding a menu to your next Bash project!
Further Resources
Happy scripting!