If you are wondering how can we check whether an environment variable is set or not in the system, here are few methods :
1. Manually
a. Run “set” command on the shell prompt, it will list out all the variables set in the system :
# set
b. Run “echo” command on the shell to print the value of the environment variable :
# echo $<environment variable>
2. Through Shell Script
Let say we intent to check whether environment variable “envVar” is set or not?
a. Method 1 – checking if “envVar” is set to “” (no value) :
if [ "$envVar" == "" ]
then
echo “envVar is not set”
else
echo “envVar is set”
fi
b.Method 2 – checking if “envVar” is non NULL :
if [ -n "$envVar" ]
then
echo “envVar has non NULL value, it is set.”
else
echo “envVar has NULL value, it is not set.”
fi
Comments
Post a Comment