Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a script that goes like this:

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


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

   if [[ $Run_Time < $Wait_Time ]]
      Delay_Time=$(( $Wait_Time - $Run_Time ))
      sleep $Delay_Time
      echo "Delay exceeded" 
      echo $Run_Time
      echo $Wait_Time

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
share|improve this question
can you paste original script – klerk Jun 23 '14 at 19:16
Unfortunately i am not allowed to do that :( But I did triple check that each variables were NOT used anywhere else – TCZ8 Jun 23 '14 at 19:19
Oh forgot about that one, its set at the top of the script. But the issue was the < operator just like godlygeek described :/ – TCZ8 Jun 23 '14 at 20:33
up vote 12 down vote accepted

Try running this code snippet:

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

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.

share|improve this answer
+1 You can also use -lt in place of <; the former is specifically for integer comparison so can be used inside []. – goldilocks Jun 23 '14 at 19:30
Ohhh man.... I'm going to have to double check my entire code :( Thanks for your help – TCZ8 Jun 23 '14 at 19:49
@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 '14 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 '14 at 19:20

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.