# How to test for number smaller than other number

I have a script that goes like this:

``````while :
do
Start_Time=\$(date +"%s")

MAIN PROGRAM GOES HERE (CROPPED TO SHORTEN THINGS)

Run_Time=\$(( \$(date +"%s") - \$Start_Time ))

if [[ \$Run_Time < \$Wait_Time ]]
then
Delay_Time=\$(( \$Wait_Time - \$Run_Time ))
sleep \$Delay_Time
else
echo "Delay exceeded"
echo \$Run_Time
echo \$Wait_Time
fi
done
``````

And my problem is that sometimes even if the run time is smaller than the wait time it fails the < test

Here is an output from the last run:

``````Delay exceeded
Run_Time 4
Wait_Time 30
``````

Try running this code snippet:

``````if [[ 5 < 20 ]]
then
echo "5 < 20, as expected"
else
echo "5 is not less than 20, but why?"
fi
``````

And the output would be `5 is not less than 20, but why?`. The answer is that you're using the `<` conditional expression operator, which is documented as doing:

```       string1 < string2
True if string1 sorts before string2 lexicographically in the current locale.
```

And your problem is that "20" is lexicographically (or, basically, alphabetically) before "5".

You're looking for:

``````if (( \$Run_Time < \$Wait_Time ))
``````

instead - this uses arithmetic evaluation and an arithmetic less-than, which is what you need.

• +1 You can also use `-lt` in place of `<`; the former is specifically for integer comparison so can be used inside `[]`. Jun 23, 2014 at 19:30
• @goldilocks your documentation on operators seems really complete, do you have a link? I really need to brush up on those. Thanks.
– TCZ8
Jun 23, 2014 at 20:31
• Thank you very much goldilock. BTW to anyone else that may land here for a similar problem, I also encountered a similar "read the manual or you wont know about it" behavior using -eq as an operator. -eq can lead to unexpected results when you are testing if a variable is equal to 0 ie.: [[ \$var -eq 0 ]] it will still test true if \$var is unset. You can use = or == instead which behaves properly. There is a LOT to learn about linux... im loving it.
– TCZ8
Jun 26, 2014 at 19:20