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.
| Form | Result when unset or empty | Result when set and not empty |
|---|---|---|
${v:-word} | word | value of v |
${v:=word} | word, and v is assigned word | value of v |
${v:+word} | nothing | word |
${v:?word} | prints word and exits | value 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.
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.
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.
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.
: "${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.
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.
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.