jeudi 14 novembre 2019

A "if" block in the bash returns wrong value

Could someone explain the following behavior of the "if" block of bash ?

I use the following simple code check if the first_value is less than the second_value

first_value=67
second_value=2

if [[  "${first_value}" < "${second_value}" ]];
then 
    echo "Yes"
else 
    echo "No"
fi

The issue is

If the second_value is 1,2,3,4,5,6,10,11,... the block will return "No"

But if the second_value is 7,8,9 the block will return "Yes" (must be "No")

The fix is to use "-lt" instead of "<" but I want to understand such behavior of the "if" block.

The bash version is "GNU bash, version 4.2.46(2)-release (x86_64-redhat-linux-gnu)"

The OS is "CentOS Linux release 7.5.1804 (Core)"

Aucun commentaire:

Enregistrer un commentaire