Safer Zsh Scripts with Parameter Expansion Defaults
Use Zsh parameter expansion defaults and pattern removal to write shorter, safer shell scripts.
17 May 2026, 03:49 UTC

A missing variable should not silently break your deploy
You inherit a Zsh deployment script. It reads IMAGE_TAG from the environment and falls back to latest with a block like this:
if [ -z "$IMAGE_TAG" ]; then
IMAGE_TAG="latest"
fi
It works until someone exports IMAGE_TAG= (an empty string). The test sees a zero-length string, treats it as missing, and substitutes latest anyway. Worse, a typo like IMGE_TAG passes through empty and the script pushes an untagged image.
Zsh parameter expansion lets you express defaults and removals directly on the variable reference, often removing the need for separate if blocks. Combined with set -o nounset, it turns silent mistakes into immediate failures.
Default values: ${VAR:-default} versus ${VAR-default}
Parameter expansion is the mechanism Zsh uses to substitute a variable's value inside a string. The ${VAR:-default} form expands to default when VAR is unset or set to an empty string. The ${VAR-default} form only substitutes when VAR is unset; an empty string passes through unchanged.
That distinction matters for configuration scripts where an empty value is intentional. Word splitting happens after expansion, so quote the whole expansion to protect spaces and glob characters. Pathname expansion, also called globbing, can also alter unquoted results.
Verify the behavior yourself
Run this in your terminal. No special permissions are required.
unset FOO
zsh -c 'print ${FOO:-default}'
Expected check: the shell prints default. If FOO is already set in your shell, unset it first or the result will reflect that value. The risk is minimal; this does not modify system state.
Strip prefixes and suffixes without subshells
Zsh also offers ${VAR#pattern} to remove the shortest matching prefix and ${VAR%pattern} to remove the shortest matching suffix. These run inside the shell, so they avoid spawning sed or cut for simple cleanup.
For example, if a CI system sets GIT_REF=refs/heads/main, you can derive the branch name with:
branch=${GIT_REF#refs/heads/}
Pattern removal happens before word splitting, but quoting the result still guards against spaces in the final value.
Worked example: a safer release script
Here is a compact script that reads a registry and image tag, applies defaults, strips a Git reference prefix, and aborts if a required variable is missing.
#!/usr/bin/env zsh
set -o nounset
REGISTRY="${REGISTRY:-registry.example.com}"
IMAGE="${IMAGE:-my-app}"
# TAG_SOURCE may be unset in local testing; abort rather than deploy blindly.
TAG_SOURCE="${TAG_SOURCE:?missing image tag source}"
# Accept refs/heads/main or plain main.
TAG="${TAG_SOURCE#refs/heads/}"
TAG="${TAG#refs/tags/}"
print "Pushing $REGISTRY/$IMAGE:$TAG"
Where to run: save as release.zsh, make it executable with chmod +x release.zsh, and run ./release.zsh. Permissions: ordinary user permissions are enough; the script only prints unless you add push commands.
Expected checks:
- TAG_SOURCE=refs/heads/v1.2.3 ./release.zsh prints Pushing registry.example.com/my-app:v1.2.3.
- Running with no TAG_SOURCE aborts with an error about the missing variable because of :? and nounset.
Risk: set -o nounset causes the shell to exit on any unset reference, so every variable used later must be initialized or defaulted first.
Limitations and what to check
Unquoted expansions can undergo word splitting and pathname expansion. If a default contains spaces or wildcard characters, quote the expansion: "${VAR:-default with spaces}". The // global replacement form can replace more substrings than intended if the pattern is too broad.
Older Zsh releases before 5.0 lack some expansion flags, but :- , # , and % are long-standing. Check your version with:
zsh --version
Expected output should report 5.0 or newer for routine scripting; 5.9 or newer gives full modern expansion support. Verify these behaviors on your Zsh version before adopting them in CI.
Add it to your script header
Start Zsh scripts with set -o nounset and replace verbose if [ -z ... ] blocks with ${VAR:-default}. Test each default by unsetting the variable and confirming the fallback appears, then confirm an unset required variable stops the script instead of continuing with bad data.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.