1827636 Members
3775 Online
109966 Solutions
New Discussion

bash: unbound variable

 
Greg B_1
New Member

bash: unbound variable

when I execute a shell script containing:
(and expecting to get one of the two statements echoed)

if [ $greg = "test" ]; then
echo "greg equal to test"
else
echo "greg not equal to test"
fi

I get:
bash: greg: unbound variable

Can anyone explain what's the deal there?

3 REPLIES 3
RAC_1
Honored Contributor

Re: bash: unbound variable

how shell is supposed to find greg? you are defining it anywhere in the script.

It should be
grep=test
if [ $greg = "test" ]; then
echo "greg equal to test"
else
echo "greg not equal to test"
fi

This will give greg eqal to test
There is no substitute to HARDWORK
Deepak Extross
Honored Contributor

Re: bash: unbound variable

An unbound variable is one that has not been defined.

Try checking for existence of the variable using the test -z option before doing any comparison:
if [ -z $greg ]; then
....
T G Manikandan
Honored Contributor

Re: bash: unbound variable

///define greg//

greg=test
or
greg=test1


if [ $greg = "test" ]
then
echo "greg equal to test"
else
echo "greg not equal to test"
fi