A simple Bash variable test goes:
${varName:? \"${varName} is not defined\"}
I\'d like to re-use this, by putting it in a function. How
This function tests for variables that ARE CURRENTLY set. The variable may even be an array. Note that in bash: 0 == TRUE, 1 == FALSE.
function var.defined {
eval '[[ ${!'$1'[@]} ]]'
}
# Typical Usage of var.defined {}
declare you="Your Name Here" ref='you';
read -p "What's your name: " you;
if var.defined you; then # Simple demo using literal text
echo "BASH recognizes $you";
echo "BASH also knows a reference to $ref as ${!ref}, by indirection.";
fi
unset you # have just been killed by a master :D
if ! var.defined $ref; then # Standard demo using an expanded literal value
echo "BASH doesn't know $ref any longer";
fi
read -s -N 1 -p "Press any key to continue...";
echo "";
So to be clear here, the function tests literal text. Everytime a command is called in bash, variables are GENERALLY 'swapped-out' or 'substituted' with the underlying value unless: