is_defined_and_set
Function description
The is_defined_and_set function may be used to test whether a named variable has
been defined and assigned a non-null value.
Function parameters
$1 - The name of the variable to test.
Return value
The is_defined_and_set function will return zero if the named variable is
defined and has been assigned a non-null value. If the named variable is undefined or has
been assigned a null value then 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_defined_and_set DEFINED_BUT_EMPTY && echo "DEFINED_BUT_EMPTY is defined and set"
is_defined_and_set DEFINED_AND_SET && echo "DEFINED_AND_SET is defined and set"
is_defined_and_set UNDECLARED_VAR && echo "UNDECLARED_VAR is defined and set"
max@lisa ~ $ ./is_defined_and_set.shDEFINED_AND_SET is defined and set