Skip to main content

How to Give a Variable a Default Value in the Terminal

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

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:

Syntax
"${NAME:-default}"
"${NAME-default}"
PartWhat 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:

Syntax
$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​

Terminal
unset CITY
echo "${CITY:-Pune}"
CITY='Mumbai'
echo "${CITY:-Pune}"
CITY=''
echo "${CITY:-Pune}"
echo "[${CITY-Pune}]"
Output
Pune
Mumbai
Pune
[]

The zsh terminal after the seven commands: Pune, Mumbai, Pune, and then empty brackets.

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 gives Pune. 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​

MistakeWhat happensDo this instead
Leaving out the colon when an empty value should also fall back${CITY-Pune} returns the empty value, not PuneUse ${CITY:-Pune}
Expecting ?? to replace an empty textIt only replaces $null, so '' stays emptyTest 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 bashQuote it: "${DIR:-Pune}"

Troubleshooting​

SymptomLikely causeFix
The default never appearsThe variable is set, perhaps to an empty value, and the form has no colonAdd the colon: :-
PowerShell shows nothing instead of the defaultThe variable holds '', not $nullCheck 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​