Parameter expansion keeps small string changes inside a Zsh script when the script already has the value in a parameter. It fits default arguments, service labels, short prefixes, and path-derived filenames that should be built before the next command runs.
The common form is ${name...}, where braces separate the parameter name from the operation applied to its value. In ${empty:-fallback}, the colon makes Zsh use the fallback when the parameter is unset or empty, while modifiers such as :h, :t, :r, and :e work on path-like strings.
The sample script runs with a default service name and with an explicit service name. The output confirms which fields follow the argument, which fallback is used for an empty value, and how the (U) flag plus path modifiers build a rotated log filename.
Related: How to use variables in Zsh
Related: How to use arrays in Zsh
Related: How to use glob qualifiers in Zsh
Related: Use parameter expansion in Bash
Steps to use parameter expansion in Zsh:
- Create and enter a temporary workspace for the example script.
$ mkdir -p /tmp/zsh-parameter-demo $ cd /tmp/zsh-parameter-demo
- Create a script with several Zsh parameter-expansion forms.
- expansion-demo.zsh
#!/usr/bin/env zsh emulate -L zsh setopt err_exit no_unset service=${1:-api-worker} empty="" log_path="/srv/app/releases/${service}.log" print -r -- "service=${service}" print -r -- "fallback=${empty:-default-service}" print -r -- "upper=${(U)service}" print -r -- "prefix=${service:0:3}" print -r -- "file=${log_path:t}" print -r -- "dir=${log_path:h}" print -r -- "rotated=${log_path:r}.1.${log_path:e}"
${1:-api-worker} supplies a default argument, (U) uppercases the value, and the path modifiers split /srv/app/releases/api-worker.log without basename or dirname.
- Check the script syntax.
$ zsh -n expansion-demo.zsh
No output means Zsh parsed the script without finding a syntax error.
- Run the script with the default service name.
$ zsh expansion-demo.zsh service=api-worker fallback=default-service upper=API-WORKER prefix=api file=api-worker.log dir=/srv/app/releases rotated=/srv/app/releases/api-worker.1.log
- Run the script with an explicit service name.
$ zsh expansion-demo.zsh frontend service=frontend fallback=default-service upper=FRONTEND prefix=fro file=frontend.log dir=/srv/app/releases rotated=/srv/app/releases/frontend.1.log
- Quote expansion results when they become command arguments.
$ log_path="/srv/app/releases/monthly report.log" $ print -r -- "${log_path:t}" monthly report.logZsh does not split ordinary scalar parameter expansions on whitespace by default, but quotes still preserve empty values and protect scripts that later use arrays or option changes. Test dense forms with print -r -- before using them with commands such as rm, chmod, or mv.
- Remove the sample workspace after testing.
$ cd /tmp $ rm -rf /tmp/zsh-parameter-demo
Mohd Shakir Zakaria is a cloud architect with deep roots in software development and open-source advocacy. Certified in AWS, Red Hat, VMware, ITIL, and Linux, he specializes in designing and managing robust cloud and on-premises infrastructures.