Interactive Prompts
Menu Choice
Map a Number
Menu prompts usually present numbered choices. A case statement keeps the mapping readable and gives unexpected choices a controlled fallback.
Program
Play the script to choose the menu number and see the selected action.
menu_choice_prompt.sh
Replay: real traced execution (multi-file project)
#!/usr/bin/env bash
choice=2
case "$choice" in
1) action="list" ;;
2) action="build" ;;
3) action="test" ;;
*) action="help" ;;
esac
echo "choice=$choice action=$action"
#!/usr/bin/env bash
choice=3
case "$choice" in
1) action="list" ;;
2) action="build" ;;
3) action="test" ;;
*) action="help" ;;
esac
echo "choice=$choice action=$action"
choice ← 2
3choice=24case "$choice" invalues this step2choicecase "$choice" in
3choice=24case "$choice" in5 1) action="list" ;;values this step2choiceaction ← build
51) action="list" ;;62) action="build" ;;73) action="test" ;;values this stepbuildactionecho "choice=$choice action=$action"
9esac10echo "choice=$choice action=$action"outputchoice=2 action=buildvalues this step2choicebuildaction
choice ← 3
3choice=34case "$choice" invalues this step3choicecase "$choice" in
3choice=34case "$choice" in5 1) action="list" ;;values this step3choiceaction ← test
62) action="build" ;;73) action="test" ;;8*) action="help" ;;values this steptestactionecho "choice=$choice action=$action"
9esac10echo "choice=$choice action=$action"outputchoice=3 action=testvalues this step3choicetestaction
menu prompt
A menu prompt asks the user to choose from a known set of options.
numeric choice
Numbers are compact for menus, but should be mapped to clear action names.
fallback branch
The `*` branch handles unexpected input with a safe fallback.