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
#!/usr/bin/env bash
choice=
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=
case "$choice" in
1) action="list" ;;
2) action="build" ;;
3) action="test" ;;
*) action="help" ;;
esac
echo "choice=$choice action=$action"
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.