xxxxxxxxxx
if [ -z "$var" ] #return true if $var is unset
xxxxxxxxxx
if [ -z "$variable" ];
then echo "$variable is null";
else echo "$variable is not null";
fi
xxxxxxxxxx
VAR=`echo Hello world`
if [[ -n "$VAR" ]] ; then echo "Variable is set" ; fi
if [[ -z "$VAR" ]] ; then echo "Variable is null" ; fi
xxxxxxxxxx
[ -z "$var" ] && echo "Empty"
[ -z "$var" ] && echo "Empty" || echo "Not empty"
OR
[[ -z "$var" ]] && echo "Empty"
[[ -z "$var" ]] && echo "Empty" || echo "Not empty"
OR
## Check if $var is set using ! i.e. check if expr is false ##
[ ! -z "$var" ] || echo "Empty"
[ ! -z "$var" ] && echo "Not empty" || echo "Empty"
[[ ! -z "$var" ]] || echo "Empty"
[[ ! -z "$var" ]] && echo "Not empty" || echo "Empty"
xxxxxxxxxx
#!/bin/bash
# Define a variable
my_variable=""
# Check if variable is empty
if [ -z "$my_variable" ]; then
echo "Variable is empty"
else
echo "Variable is not empty"
fi