Jigar
Jigar

Reputation: 478

Unexpected Output of Shell Script

I was going through old tutorials and I found one shell script:

a=10
b=20
if  $a -eq $b 
then
   echo "a is equal to b"
else
   echo "a is not equal to b"
fi

When I stored this shell script as test.sh and executed it. It gives me below output

test.sh: 3: test.sh: 10: not found
a is not equal to b

I don't understand the first line of the output. I would appreciate if anyone can throw light on what's going on.

Upvotes: 2

Views: 105

Answers (1)

Filkolev
Filkolev

Reputation: 460

The if expects a test, like this: [ $a -eq $b ] or [[ $a -eq $b ]].

Your sample expands to: if 10 -eq 20 at which point bash looks for a program called 10 which obviously doesn't exist, hence the error message.

A missing program apparently evaluates as false which leads to the execution of the else statement.

Upvotes: 2

Related Questions