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.

choice
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"
  1. choice ← 2

    3choice=24case "$choice" in
    values this step2choice
  2. case "$choice" in

    3choice=24case "$choice" in5    1) action="list" ;;
    values this step2choice
  3. action ← build

    51) action="list" ;;62) action="build" ;;73) action="test" ;;
    values this stepbuildaction
  4. echo "choice=$choice action=$action"

    9esac10echo "choice=$choice action=$action"
    outputchoice=2 action=build
    values this step2choicebuildaction
  1. choice ← 3

    3choice=34case "$choice" in
    values this step3choice
  2. case "$choice" in

    3choice=34case "$choice" in5    1) action="list" ;;
    values this step3choice
  3. action ← test

    62) action="build" ;;73) action="test" ;;8*) action="help" ;;
    values this steptestaction
  4. echo "choice=$choice action=$action"

    9esac10echo "choice=$choice action=$action"
    outputchoice=3 action=test
    values 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.