bash script -eq : equal comparison operator in bash


Hi all,

today i will give you a small introduction to a comparison operator in bash script – the -eq operation. Eq stands for Equal, and like the name said, we compare two numbers if they are equal or not.

 

 

First example:

if [ 0 -eq 1 ]
then
echo „Something is wrong“
else
echo „Yeah right – 1 is greater then 0“
fi

In this small linux script we compare only two numbers: 0 and 1, and surprise, because 0 < 1 the if-statement returns wrong.

Second example:

while [  1 -eq 1 ]
do
echo „Yeah an endless loop“
done

We created an endless loop, this maybe useful if we write a unix or linux daemon or another script, that we don’t want to stop.

The -eq operator works only for numbers, if you want compare strings, you have to work with = or !=.

Kommentare sind abgeschaltet.