Bash shell find out if a variable has NULL value OR not

Originally published at: https://www.cyberciti.biz/faq/bash-shell-find-out-if-a-variable-has-null-value-or-not/

I am a new to shell scripting. How do I check for NULL value in a Linux or Unix shell scripts?

You can use this way:

my_str="test"
if [[ ${my_str} ]]; then
  echo " Has a value"
else
  echo "No value"
fi
1 Like

This would have worked:

[ "$my_str" ] && echo Has a value || echo No value