Common problems and their solutions
(, en)
What is null, what is unset
As soon as you declare or assing a var, it is set.
var=
var=''
var=""
var=$''
var=$""
var=$(true) # or any command that outputs nothing or only newline characters
var=${null_or_unset_var}
Default values
- no colon » test for unset
- with colon » test for unset and null
# null as default value
K8S_CLUSTER="${K8S_NAMESPACE-}-cluster1" # If variable unset, use default.
K8S_USER="${K8S_NAMESPACE:-}-user1" # If variable unset or null, use default.
# non-null default value
K8S_CLUSTER="${K8S_NAMESPACE-abc}-cluster1"
K8S_USER="${K8S_NAMESPACE:-abc}-user1"
# default with assignment
: "${K8S_NAMESPACE:=ns1}"
: "${DRY_RUN:=}"
: "${DRY_RUN=}"