Check if a variable has a value assigned?

Hi,

I want to check if a variable has a value assigned to it or not.

I can do following -

cat $Var > File1
if [ -s File1 ]
then
      echo "$Var has value"
else
      echo "$Var is null"
fi

But I have to check for 3 Variables and I want to wrap it up in couple of unix statements.

Any suggestion is appreciated.

Thanks
Sumeet

Hi Sumeet,

It may be personal preference, but I would do something more like the following when checking if a variable has contents. Then you can easily check all three rather than writing files that will probably have to be clean up. Also, you can't always be sure that you can write a file. You may not have permissions or the filesystem could be full etc.

if [ "X"$Var == "X" ]
then
      echo "\$Var in NULL"
else
      echo "\$Var has a value of $Var"
fi

man test

if [ -z $Var ]
then
      echo '$Var is null'
else
      echo '$Var has value'
fi
if [ -z "${var+X}" ]
then
  echo var is unset
elif [ -z "$var" ]
then
  echo var is set but empty
else
  echo "The value of \$var is $var"
fi