Return true if a bash variable is unset or set to the null (empty) string:
if [ -z "$var" ]; then echo "NULL"; else echo "Not NULL"; fi
Another option to find if bash variable set to NULL:
[ -z "$var" ] && echo "NULL" [ -z "$my_var" ] && echo "NULL" || echo "Not NULL" [[ -z "$my_var" ]] && echo "NULL" [[ -z "$my_var" ]] && echo "NULL" || echo "Not NULL"
Check if $my_var is NULL using ! so expr is false
[ ! -z "$my_var" ] || echo "NULL" [ ! -z "$my_var" ] && echo "Not NULL" || echo "NULL" [[ ! -z "$my_var" ]] || echo "NULL" [[ ! -z "$my_var" ]] && echo "Not NULL" || echo "NULL"
The -n returns TRUE if the length of STRING is nonzero
if [ ! -n "$var" ] then echo "NULL"; else echo "Not NULL"; fi
—
return to gimbo wiki home page