How to Give a Variable a Default Value in the Terminal
Commands often read a setting from a variable that may not be there: a folder, a city, a port. Instead of failing, they can fall back to a default. zsh and bash write that as ${NAME:-default}. PowerShell uses the ?? operator. The two differ in one detail, an empty variable, which this guide shows with real output.
Syntax
In zsh and bash:
"${NAME:-default}"
"${NAME-default}"
| Part | What it means |
|---|---|
${NAME:-default} | NAME's value, or default when NAME is unset or empty |
${NAME-default} | NAME's value, or default only when NAME is unset |
The zsh manual describes both forms together: "If name is set, or in the second form is non-null, then substitute its value; otherwise substitute word." The second form is the one with the colon.
In PowerShell:
$name ?? 'default'
Microsoft's documentation says the ?? operator "returns the value of its left-hand operand if it isn't null. Otherwise, it evaluates the right-hand operand and returns its result."
The Example
A CITY variable falls back to Pune. You read it three times: when it's unset, when it's Mumbai, and when it's an empty value.
Step 1: Try the Default in Three Situations
- macOS / Linux
- Windows (PowerShell)
unset CITY
echo "${CITY:-Pune}"
CITY='Mumbai'
echo "${CITY:-Pune}"
CITY=''
echo "${CITY:-Pune}"
echo "[${CITY-Pune}]"
Pune
Mumbai
Pune
[]
$city = $null
$city ?? 'Pune'
$city = 'Mumbai'
$city ?? 'Pune'
$city = ''
"[$($city ?? 'Pune')]"
Pune
Mumbai
[]

With no CITY, the default Pune came back. With CITY='Mumbai', its own value won. With an empty CITY, the two shells part ways:
- zsh and bash, with the colon:
${CITY:-Pune}treats empty like unset and givesPune. Without the colon,${CITY-Pune}keeps the empty value, so the brackets are empty. - PowerShell: an empty text isn't
$null, so??keeps it, and the brackets are empty.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
| Leaving out the colon when an empty value should also fall back | ${CITY-Pune} returns the empty value, not Pune | Use ${CITY:-Pune} |
Expecting ?? to replace an empty text | It only replaces $null, so '' stays empty | Test for empty yourself, for example if (-not $city) { $city = 'Pune' } |
Leaving out the double quotes: ${DIR:-Pune} | Fine for one word. A value with spaces, such as my folder, stays one piece in zsh but is split into my and folder in bash | Quote it: "${DIR:-Pune}" |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
| The default never appears | The variable is set, perhaps to an empty value, and the form has no colon | Add the colon: :- |
| PowerShell shows nothing instead of the default | The variable holds '', not $null | Check for empty text, not only for $null |
FAQ
Does ${NAME:-default} change the variable?
No. It only uses the default for that one expansion. To also store the default, zsh and bash have ${NAME:=default}.
Where would I use this?
Anywhere a command has a sensible fallback, such as a folder that defaults to your home folder: "${DATA_DIR:-$HOME/data}".
Conclusion
${CITY:-Pune} gave Pune when CITY was unset or empty and Mumbai when it was set, while PowerShell's ?? only replaced $null. Use the colon form when an empty value should also fall back.
Additional Resources
- zsh parameter expansion — every
${...}form. - about_Operators — the null-coalescing operators
??and??=.
