Bash select Built-in Interactive Menu Prompt
Build a dependency-free interactive menu using the bash select built-in and PS3 with input validation and a re-prompt loop
- Target user
- Sysadmins and platform engineers writing operator-facing shell scripts
- Difficulty
- Beginner
- Tools
- Claude, ChatGPT, Cursor
The prompt
You are a senior shell engineer. Write a portable bash script that presents an interactive menu using only the `select` built-in and the `PS3` prompt variable — do NOT use whiptail, dialog, or any external TUI dependency. Follow these steps:
1. Set strict mode (`set -euo pipefail`) and define a `PS3` prompt string of [PROMPT_TEXT] (e.g. "Select an action: ").
2. Define the menu options as a bash array named `options`, populated from [MENU_OPTIONS] (e.g. "Restart service", "Tail logs", "Show status"), and always append a final "Quit" entry.
3. Wrap the `select choice in "${options[@]}"; do ... done` loop so it re-prompts indefinitely until the user explicitly chooses Quit.
4. Inside the loop, validate input: if `$REPLY` is empty or out of range (`$choice` is unset), print a clear "Invalid selection, try again" message to stderr and `continue` the loop rather than exiting.
5. Use a `case "$choice" in` block to dispatch each option to a dedicated function named after the action; call the action handler [ACTION_HANDLER] as the body placeholder for real work.
6. On the "Quit" branch, `break` out of the loop and exit 0; ensure Ctrl-D (EOF) also exits cleanly without an error.
Output format: a single self-contained `.sh` file with a shebang, an inline comment above each section, and no code fences in the file itself.
Guardrail: the script must be safe to re-run and must never perform a destructive action without an explicit confirmation sub-prompt; reading input or showing the menu has no side effects, and selecting Quit or EOF must leave the system unchanged.
Why this prompt works
The bash select built-in is one of the most underused tools in the shell toolkit. Most engineers reach for whiptail or dialog the moment they need a menu, pulling in an external dependency that may not exist on a minimal container or a freshly provisioned host. By explicitly forbidding those tools and steering the model toward select plus the PS3 variable, this prompt produces a menu that runs anywhere bash itself runs — no packages, no terminal capability negotiation, no surprises in a CI shell.
The numbered steps force the model to handle the parts beginners usually forget: the re-prompt loop, the empty-or-out-of-range $REPLY case, and clean EOF handling. select does not validate input for you — pressing Enter on a blank line or typing a number past the last option leaves $choice unset and silently loops, which confuses operators. Demanding an explicit “Invalid selection” branch and a continue turns that footgun into a friendly retry, and routing each option through a named function keeps the dispatch case readable as the menu grows.
The guardrail is what makes this safe for real operations work. A menu that maps number 4 to “delete all backups” is an outage waiting on a fat finger. By requiring that destructive actions sit behind a second confirmation read, and that simply opening or quitting the menu has zero side effects, the resulting script is idempotent at the navigation level — operators can explore it freely and only commit to consequences when they deliberately confirm.
Related prompts
-
Bash getopts Long-Options Parser Prompt
Build a robust Bash option parser that supports both short flags and GNU-style long options with validation and a generated usage block
-
Bash Interactive TUI Menu with whiptail Prompt
Wrap a risky operational script in a guided whiptail/dialog menu — checklists, confirmations, input validation, and a non-interactive flag — so on-call engineers run it safely at 3am without memorizing flags.
-
Bash Script Code Review Prompt
Get a senior-engineer review of any Bash script — safety, idempotency, error handling, portability.