Bash Scripting — Menu-Driven Scripts
Welcome to Chapter 28 — Menu-Driven Scripts 🎛️
Command-line tools don’t have to be boring walls of text. A well-crafted menu turns a pile of scripts into a polished, user-friendly toolkit that anyone on your team can operate — no documentation required.
In this chapter you’ll learn how to build interactive, menu-driven Bash scripts from the ground up.
What You’ll Learn
| Lesson | Topic | Link |
|---|---|---|
| 01 | Full CLI Menu with case |
Full CLI Menu |
| 02 | Sub-Menus | Sub-Menus |
| 03 | Dynamic Menus from Arrays | Dynamic Menus |
| 04 | Color and ANSI Codes | ANSI Colors |
| 05 | Exit and Quit Handling | Exit & Quit |
| 06 | Practice Exercises | Exercises |
Why Menu-Driven Scripts?
Think about the last time you handed a Bash script to a colleague. Did they know which arguments to pass? Probably not. Menus solve that:
- Discoverability — users see every option at a glance.
- Safety — invalid choices are caught before damage is done.
- Professionalism — a clean menu feels like a real application.
Quick Preview
Here’s a taste of what you’ll be building by the end of this chapter:
#!/usr/bin/env bash
set -euo pipefail
PS3="Choose an option: "
options=("Show Disk Usage" "Show Memory" "Quit")
select opt in "${options[@]}"; do
case "$opt" in
"Show Disk Usage") df -h ;;
"Show Memory") free -m ;;
"Quit") echo "Bye!"; break ;;
*) echo "Invalid option $REPLY" ;;
esac
done
By Lesson 06, you’ll add colors, sub-menus, dynamic options, and graceful exit handling on top of this foundation.
Prerequisites
Before starting this chapter, make sure you’re comfortable with:
- Functions — we’ll use them heavily for sub-menus.
casestatements — the backbone of menu dispatch.- Loops —
whileandselectloops drive the menu cycle. read— capturing user input.
Conventions Used
| Convention | Meaning |
|---|---|
$ |
Regular user prompt |
# |
Root prompt (use with caution) |
→ |
Expected output |
| 💡 | Helpful tip |
| ⚠️ | Watch out — common mistake |
Ready? Let’s start with Lesson 01 — Full CLI Menu with case. 🚀