esac
Marks the end of a case statement
TLDR
View documentation for the case keyword
SYNOPSIS
case WORD in PATTERN1|PATTERN2) COMMANDS1 ;; PATTERN3) COMMANDS2 ;; *) DEFAULT_COMMANDS ;; esac
DESCRIPTION
esac is a reserved word in POSIX-compliant shells like Bash, Zsh, and Dash, used exclusively to close a case construct. It marks the end of pattern-matching branches within a case statement, enabling conditional execution based on word pattern matching.
The case statement evaluates a word against multiple patterns, executing commands in the first matching branch. Each branch ends with ;;, and the entire structure terminates with esac ("case" spelled backwards for readability). This construct is more efficient than nested if-elif statements for multiple discrete conditions.
Common use cases include argument parsing in scripts (e.g., handling -h, --help), menu systems, or configuration selectors. Patterns support wildcards like * (default), ?, and globbing, with | for alternatives.
esac has no options, arguments, or standalone invocation—it's purely syntactic. Misuse (e.g., omitting it) causes parse errors like syntax error near unexpected token. It's ubiquitous in shell scripting for its conciseness and portability.
CAVEATS
Cannot be used standalone; requires matching case. Indentation optional but recommended. Patterns are glob-style, not regex. Fallthrough unsupported without && or ;& (Bash-specific).
EXAMPLE
case "$1" in start) echo "Starting service" ;; stop) echo "Stopping service" ;; *) echo "Usage: $0 {start|stop}" ;; esac
PATTERN MATCHING
Supports * (any), ? (single char), [abc] (sets), ! (negation), | (OR). Quoted patterns literal.
HISTORY
Originated in the Bourne shell (1977) by Stephen Bourne at Bell Labs. Standardized in POSIX.1-1988, with extensions in Bash (1989) like ;& fallthrough in Bash 4.0 (2009).


