is_defined
Function description
The is_defined function may be used to test whether a named variable has
been defined, even if it currently holds a null value.
Function parameters
$1 - The name of the variable to test.
Return value
The is_defined function will return zero if the named variable is defined
(even if it contains a null value) or non-zero if the named variable is undefined.
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_defined DEFINED_BUT_EMPTY && echo "DEFINED_BUT_EMPTY is defined"
is_defined DEFINED_AND_SET && echo "DEFINED_AND_SET is defined"
is_defined UNDECLARED_VAR && echo "UNDECLARED_VAR is defined"
max@lisa ~ $ ./is_defined.shDEFINED_BUT_EMPTY is defined
DEFINED_AND_SET is defined