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
#! /bin/bash
# We rely on some functions from hacking-bash.sh
[[ -r ${HACKING_BASH_LIB_PATH:=/usr/lib/hacking-bash.sh} ]] && \
source ${HACKING_BASH_LIB_PATH} || \
{ echo "Unable to find ${HACKING_BASH_LIB_PATH}"; exit 1; }
# Create some variables
DEFINED_BUT_EMPTY=""
DEFINED_AND_SET="hello!"
unset UNDECLARED_VAR
# Test them
is_undefined_or_unset DEFINED_BUT_EMPTY && echo "DEFINED_BUT_EMPTY is undefined or unset"
is_undefined_or_unset DEFINED_AND_SET && echo "DEFINED_AND_SET is undefined or unset"
is_undefined_or_unset UNDECLARED_VAR && echo "UNDECLARED_VAR is undefined or unset"
max@lisa ~ $ ./is_undefined_or_unset.shDEFINED_BUT_EMPTY is undefined or unset
UNDECLARED_VAR is undefined or unset