$_ CLI In Depth
Learning progress 0%
lesson functions arrays parameter-expansion

Defaults & Alternates: ${var:-x} and ${var:?}

4 min read Module 14: Functions, Arrays & Parameter Expansion Updated Dec 5, 2026
Cheat Sheet
0:00 / 3:21

The four forms with a colon

Each form in this family has two variants. The version with a colon treats an empty value the same as an unset variable. The version without a colon treats only an unset variable as missing. Most scripts want the colon version, because an empty string is rarely a useful value for a path, a host, or a count.

FormResult when unset or emptyResult when set and not empty
${v:-word}wordvalue of v
${v:=word}word, and v is assigned wordvalue of v
${v:+word}nothingword
${v:?word}prints word and exitsvalue of v

The :- form does not change the variable. It only supplies a value for this one expansion. The := form also assigns, which is convenient for setting a default that later code reads. The :+ form is the reverse: it produces a word only when the variable has a value, which is useful for building optional command-line flags.

Defaults with :-

The default form is the one you will use most. It lets a script run with sensible settings and lets the caller override them from the environment.

BASH6 lines
unset port
echo "port=${port:-8080}"
port=9000
echo "port=${port:-8080}"
port=""
echo "port=${port:-8080}"

The output is port=8080, then port=9000, then port=8080 again. The empty value falls back to the default because of the colon. Without the colon, the last line would print an empty port.

Assigning a default with :=

The assignment form sets the variable as well. It is useful in a script that wants every later line to see a value, for example a log directory that should exist even if the caller did not set one. The expansion itself is the same as the default form, so it returns the value it assigned.

BASH3 lines
unset logdir
echo "using ${logdir:=/tmp/logs}"
echo "now logdir=$logdir"

The first line prints using /tmp/logs, and the second line shows that the variable now holds the same value. Positional parameters and special parameters cannot be assigned this way, so use it only for ordinary names.

Alternates with :+

The alternate form is a convenient way to add a flag only when a variable is set. The word after the plus sign is used when the variable has a non-empty value, and nothing is produced otherwise. This lets a command receive an option only when it is needed, without an if block around the call.

BASH3 lines
verbose=1
unset quiet
echo "[${verbose:+-v}] [${quiet:+-q}]"

The output is [-v] []. The verbose flag is included because the variable is set, and the quiet flag is absent. The brackets show the difference clearly, and in a real command the flag would be placed directly before the arguments.

Required values with :?

The error form stops the script when a variable is missing or empty. It prints the message after the question mark to standard error, prefixed by the script name and line, and exits with a non-zero status. This turns a silent misconfiguration into an immediate, explained failure. The usual idiom is to use it with the no-op command :, which accepts arguments and does nothing.

BASH2 lines
: "${DEPLOY_ENV:?set DEPLOY_ENV first}"
echo "deploying to $DEPLOY_ENV"

If DEPLOY_ENV is set, the script prints the deploy message. If it is missing, the error appears and the echo never runs. Run this in a terminal and see the message, because when a script calls this check, the error is the only feedback the caller gets. In an interactive shell the error does not close the terminal, but in a script it ends the script.

Tip

Put the message in the expansion, such as ${1:?usage: script FILE}. The text tells the next person exactly what the script expects, which is far more useful than a bare error about an unset variable.

Hands-on: test all four forms

Run the block below and predict each line before you read the output. The lab for this unit writes these forms into a script, so you can test them when the variables are set from outside the shell.

BASH3 lines
unset a; b=""; c=set
echo "[${a:-A}] [${b:-B}] [${c:-C}]"
echo "[${a:+yes}] [${c:+yes}]"

The output is [A] [B] [set] and then [] [yes]. The first line shows defaults for missing and empty values, and the value of the set variable is kept. The second line shows the alternate form, which produces nothing for missing and empty values.

Parameter expansion on one value

Visualizer

Parameter expansion changes the value of a variable without starting a subprocess. Each operator is a small pattern or rule applied to the same string.

1 / 6
path=/var/log/app.tar.gz
expressionresultmeaning
${path}/var/log/app.tar.gzthe value
${#path}19length in characters
${path#*/}var/log/app.tar.gzdrop shortest prefix matching */
${path##*/}app.tar.gzdrop longest prefix matching */ (basename)
${path%.*}/var/log/app.tardrop shortest suffix matching .*
${path%%.*}/var/log/appdrop longest suffix matching .*
${path/log/LOG}/var/LOG/app.tar.gzreplace first match
${path//./_}/var/log/app_tar_gzreplace every match
${path^^}/VAR/LOG/APP.TAR.GZuppercase everything
${path:1:3}varoffset 1, length 3

Start with the value. ${path} gives it back, and ${#path} counts its 19 characters, slashes and dots included.

path=/var/log/app.tar.gz; echo "${#path} ${path##*/} ${path%%.*} ${path//./_} ${path:1:3} ${path^^}"
Terminal lab · 2 steps · real Linux in your browser

Defaults, alternates and required values

Use the default and alternate forms, then make a script refuse to run without a required variable.

01
Write the default and alternate lines
Create defaults.sh in /workspace. Unset name, then print hello with a default of world. Set name to the empty string and print an empty fallback. Set name to Ana and print yes through the alternate form: ``bash cat > defaults.sh <<'EOF' unset name echo "hello ${name:-world}" name="" echo "empty: ${name:-fallback}" name=Ana echo "set: ${name:+yes}" EOF ``
Show hint for step 1
The colon forms treat the empty string the same as unset. Use :- for the default and :+ for the alternate.
02
Require DEPLOY_ENV
Create required.sh in /workspace. It must stop with the message DEPLOY_ENV must be set when the variable is missing, and otherwise print deploying to followed by the value: ``bash cat > required.sh <<'EOF' : "${DEPLOY_ENV:?DEPLOY_ENV must be set}" echo "deploying to $DEPLOY_ENV" EOF `` Test it once with env -u DEPLOY_ENV bash required.sh and once with env DEPLOY_ENV=prod bash required.sh.
Show hint for step 2
The error form does the work. The colon command at the start consumes the expansion without printing it.
Check yourself Question 1 of 4
Concept Check Assign a default recall

Which expansion assigns a default value to the variable when it is unset or empty?

Recap & Key Takeaways

${v:-word} supplies a default for an unset or empty variable without changing it, and ${v:=word} also assigns the default.
${v:+word} produces word only when v is set and not empty, which is a clean way to add an optional flag.
${v:?message} prints the message and exits a script when a required variable is missing, so failures are early and explained.
The colon form treats empty values as missing, which is almost always what a script wants.
Was this lesson helpful? Spot an issue or have a suggestion? Let us know on GitHub.
Report an issue