marcus-g
5/5/2017 - 9:31 PM

The -z flag determines if the variable is unset. Below, where ${var+x} is a parameter expansion which evaluates to nothing if var is unset,

The -z flag determines if the variable is unset. Below, where ${var+x} is a parameter expansion which evaluates to nothing if var is unset, and substitutes the string x otherwise.

Quotes Digression

Quotes can be omitted (so we can say ${var+x} instead of "${var+x}") because this syntax & usage guarantees this will only expand to something that does not require quotes (since it either expands to x (which contains no word breaks so it needs no quotes), or to nothing (which results in [ -z ], which conveniently evaluates to the same value (true) that [ -z "" ] does as well)).

From http://stackoverflow.com/questions/3601515/how-to-check-if-a-variable-is-set-in-bash

if [ -z ${var+x} ]; then 
  echo "var is unset" # 'x' is evaluated since unset
else 
  echo "var is set to '$var'" # 'x' isn't evaluated
fi