Check the variable is a whole number
if [ $VARIABLE -eq $VARIABLE 2> /dev/null ]; then echo $VARIABLE is a number else echo $VARIABLE is not a number fi
Check the variable is not empty
if [ $VARIABLE != "" ] ; then <code_to_execute> else echo "Error: VARIABLE is empty" fi
or as provided by solstice:
if [ -z "$VARIABLE" ] ; then echo “VARIABLE is empty” else echo “VARIABLE is not empty” fi
or -n to test if it”s not null
if [ -n "$VARIABLE" ] ; then echo “VARIABLE is not empty” else echo “VARIABLE is empty” fi
Check the variable is alpha only
if [[ "$VARIABLE" =~ ^[a-zA-Z]+$ ]] ; then echo "Variable contains letters only" else echo "Error" fi
to check for an empty variable, you can use -z
if [ -z “$VARIABLE” ] ; then
echo “$VARIABLE is empty”
else
echo “$VARIABLE is not empty”
fi
or -n to test if it”s not null
if [ -n “$VARIABLE” ] ; then
echo “$VARIABLE is not empty”
else
echo “$VARIABLE is empty”
fi
-n can be confusing so I stick with -z only.
Thanks soltstice, I’ll add when I’m back at my desk 😉