Software Dev
Software Dev

Reputation: 1102

Bash Comparison with Subtraction

How do I do this in bash:

while (var1 < (var2 - 1)) {
    ...
}

Right now, this is what I have in bash:

while [ $var1 < $var2-1 ]
do
    ...
done

Upvotes: 2

Views: 284

Answers (2)

Barmar
Barmar

Reputation: 781726

Use double parentheses to make an arithmetic statement.

while ((var1 < (var2 - 1)))
do
    ...
done

Upvotes: 7

Regis Barbosa
Regis Barbosa

Reputation: 111

since

a=2; b=3; [ $a -gt $(($b-1)) ] && echo yes || echo no

then the script would be like

while [ $var1 -lt $(($var2-1)) ]; do
  ...
done

Upvotes: 0

Related Questions