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.
#!/usr/bin/env bash set -u action=${1:-status}
The - form in ${1:-status} selects status when the first argument is missing or empty.
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.
status) printf 'service is running\n' ;;
The unquoted text before ) is a shell pattern, not a string comparison expression.
restart|reload) printf 'refreshing service\n' ;;
The | separator sends either recognized word to the same branch without duplicating its commands.
*) 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.
#!/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
$ bash -n service-action.sh
No output means Bash parsed the completed file without finding a syntax error.
$ bash service-action.sh service is running
$ bash service-action.sh start starting service
$ bash service-action.sh reload refreshing service
$ bash service-action.sh remove unknown action: remove
$ 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.