Default/Fallback Argument Handling
In this page:
Providing a Fallback with :-
${var:-default} expands to var's value if it is set and non-empty, or to default otherwise, without modifying var itself. This is the most common way to give an argument a sensible default.
Example: Providing a Fallback with :-
#!/bin/bash
set --
name=${1:-"anonymous"}
echo "Hello, $name"
Login to try C/C++/Java/PHP code in the editor
Assigning the Default Back with :=
${var:=default} behaves like :- but also assigns default into var, so subsequent uses of var see the default too. This only works on ordinary variables, not positional parameters like $1.
Note: The leading : (a no-op command) is a common trick to apply := for its side effect without needing to use the expanded value anywhere.
Example: Assigning the Default Back with :=
#!/bin/bash
unset timeout
: "${timeout:=30}"
echo "timeout is now set to: $timeout"
Login to try C/C++/Java/PHP code in the editor
The Inverse Case with :+
${var:+alt} is the mirror image of :-: it expands to alt only when var is set and non-empty, and to nothing otherwise, which is useful for conditionally including a flag only when a variable is present.
Example: The Inverse Case with :+
#!/bin/bash
verbose="1"
flag=${verbose:+"--verbose"}
echo "flag is: $flag"
unset verbose
flag=${verbose:+"--verbose"}
echo "flag is now: [${flag}]"
Login to try C/C++/Java/PHP code in the editor
Combining Defaults with Validation
Defaults and required-value checks can be combined: give optional settings a fallback with :-, while still using :? for values that truly must be provided by the caller.
Example: Combining Defaults with Validation
#!/bin/bash
set -- "myapp"
app_name=${1:?app name is required}
log_level=${2:-"info"}
echo "starting $app_name with log level $log_level"
Login to try C/C++/Java/PHP code in the editor
- Confusing
${var:-default}(use default if unset OR empty) with${var-default}(use default only if unset, empty string is kept as-is). - Using
${var:=default}expecting it to work on positional parameters directly; assignment forms can't assign to$1,$2, etc., only to named variables. - Forgetting
${var:+alt}is the inverse case: it substitutesaltonly whenvarIS set and non-empty, which is easy to mix up with:-.
${var:-default}yieldsdefaultifvaris unset or empty, without changingvaritself.${var:=default}does the same but also assignsdefaultback intovar(only works on plain variables, not positional parameters).${var:+alt}yieldsaltonly whenvarIS set and non-empty; otherwise yields nothing.${var:?message}errors out immediately withmessageifvaris unset or empty.
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first:
- getopts for Parsing Command-Line Flags
- Positional Parameters and shift
- Process Substitution in Practice (diff <(...) <(...))
- Arrays of Arguments ("$@" vs "$*" Quoting Difference)
- Nameref Variables (declare -n) for Passing Arrays by Reference
- Heredoc Templating (Generating a Config File from Variables)
- Default/Fallback Argument Handling