A shell script often needs to route one input through a fixed set of named choices. A Bash case statement keeps those choices in one block and runs only the first branch whose pattern matches the input.

The shell tests patterns from top to bottom. Each branch begins with a pattern followed by ) and ends with ;;; | lets several patterns share one branch, while * catches values that no earlier branch accepted.

A service-action script makes the routing visible without changing a real service. It selects status when no argument is supplied, prints a branch-specific message for recognized actions, and returns exit status 2 for an unsupported action.

Steps to use a case statement in Bash:

  1. Create service-action.sh with its interpreter line, unset-variable mode, and default action.
    service-action.sh
    #!/usr/bin/env bash
    set -u
     
    action=${1:-status}

    The - form in ${1:-status} selects status when the first argument is missing or empty.

  2. Add the start and stop branches below the action assignment.
    case "$action" in
        start)
            printf 'starting service\n'
            ;;
        stop)
            printf 'stopping service\n'
            ;;

    Only the first matching branch runs. Ending each branch with ;; prevents matching from continuing into the next branch.

  3. Add the default status branch after the stop branch.
        status)
            printf 'service is running\n'
            ;;

    The unquoted text before ) is a shell pattern, not a string comparison expression.

  4. Add the shared restart|reload pattern after the status branch.
        restart|reload)
            printf 'refreshing service\n'
            ;;

    The | separator sends either recognized word to the same branch without duplicating its commands.

  5. Complete the case block with a catch-all branch and esac.
        *)
            printf 'unknown action: %s\n' "$action" >&2
            exit 2
            ;;
    esac

    The catch-all * pattern belongs last because it also matches every recognized action. Unsupported input returns status 2, whereas a recognized branch returns 0.

  6. Compare service-action.sh with the consolidated file.
    service-action.sh
    #!/usr/bin/env bash
    set -u
     
    action=${1:-status}
     
    case "$action" in
        start)
            printf 'starting service\n'
            ;;
        stop)
            printf 'stopping service\n'
            ;;
        status)
            printf 'service is running\n'
            ;;
        restart|reload)
            printf 'refreshing service\n'
            ;;
        *)
            printf 'unknown action: %s\n' "$action" >&2
            exit 2
            ;;
    esac
  7. Check service-action.sh for Bash syntax errors.
    $ bash -n service-action.sh

    No output means Bash parsed the completed file without finding a syntax error.

  8. Run the default status branch.
    $ bash service-action.sh
    service is running
  9. Run the exact start pattern.
    $ bash service-action.sh start
    starting service
  10. Run the shared reload pattern.
    $ bash service-action.sh reload
    refreshing service
  11. Run the unsupported remove action.
    $ bash service-action.sh remove
    unknown action: remove
  12. Print the unsupported action's exit status immediately.
    $ printf 'exit=%s\n' "$?"
    exit=2

    Status 2 confirms that the catch-all branch rejected the input. Running another command before this check replaces $? with that command's status.