A shell script often needs to turn one short input into a defined action without scattering string tests across the file. A Zsh case statement keeps those choices together and stops at the first pattern that matches.
Each branch begins with a pattern followed by ) and ends with ;;. Zsh accepts a parenthesized pattern such as (prod|production), while the | separator lets either word select the same branch.
The target router keeps the selected URL in one variable and reserves exit status 2 for invalid input. A syntax check catches an unfinished branch before execution, a recognized alias prints its assigned URL, and the catch-all branch provides fail-capable proof for an unsupported target.
Related: How to use conditionals in Zsh
Related: How to use getopts in Zsh
Related: Use a case statement in Bash
#!/usr/bin/env zsh
setopt ERR_EXIT NO_UNSET
target=${1:-}
The :- form leaves target empty when no first argument was supplied, and NO_UNSET still catches other accidental reads of undefined parameters.
case $target in
(dev|development)
url=https://dev.example.com
;;
(stage|staging)
url=https://staging.example.com
;;
The opening parenthesis is valid Zsh case syntax. Each | pattern sends two accepted names to one assignment.
(prod|production)
url=https://www.example.com
;;
("")
print -u2 -- "missing target"
exit 2
;;
(*)
print -u2 -- "unknown target: $target"
exit 2
;;
esac
A leading * branch matches every target and prevents later specific branches from running.
print -r -- "target=$target" print -r -- "url=$url"
$ zsh -n deploy-target.zsh
No output means Zsh parsed the completed case statement without finding a syntax error.
$ zsh deploy-target.zsh prod target=prod url=https://www.example.com
$ zsh deploy-target.zsh sandbox unknown target: sandbox
$ printf 'exit=%s\n' "$?" exit=2
A status of 2 distinguishes rejected input from a recognized branch, which returns 0. The shell replaces $? after any later command.