is_undefined_or_unset

Function description

The is_undefined_or_unset function may be used to test whether a named variable is undefined or is currently assigned a null-value.

Function parameters

$1 - The name of the variable to test.

Return value

The is_undefined_or_unset function will return zero if the named variable is undefined or has been assigned a null-value. If the named variable has been defined a non-zero value will be returned.

Example use

Listing 1
  1. #! /bin/bash
  2.  
  3. # We rely on some functions from hacking-bash.sh
  4. [[ -r ${HACKING_BASH_LIB_PATH:=/usr/lib/hacking-bash.sh} ]] && \
  5.     source ${HACKING_BASH_LIB_PATH} || \
  6.     { echo "Unable to find ${HACKING_BASH_LIB_PATH}"; exit 1; }
  7.  
  8. # Create some variables
  9. DEFINED_BUT_EMPTY=""
  10. DEFINED_AND_SET="hello!"
  11. unset UNDECLARED_VAR
  12.  
  13. # Test them
  14. is_undefined_or_unset DEFINED_BUT_EMPTY && echo "DEFINED_BUT_EMPTY is undefined or unset"
  15. is_undefined_or_unset DEFINED_AND_SET && echo "DEFINED_AND_SET is undefined or unset"
  16. is_undefined_or_unset UNDECLARED_VAR && echo "UNDECLARED_VAR is undefined or unset"
Example code demonstrating the is_undefined_or_unset function
max@lisa ./is_undefined_or_unset.sh
DEFINED_BUT_EMPTY is undefined or unset 
UNDECLARED_VAR is undefined or unset