0

Is there any significant difference between using [[ $a -lt 2 ]] and (( $a < 2 ))?

For example, is one of them faster or more POSIX compliant than the other?

Alexej Magura
  • 4,356
  • 7
  • 26
  • 39

1 Answers1

5

Neither is POSIX-compatible. In a POSIX shell, you can use the command [ "$a" -lt 2 ] or the expression $(( a < 2 )).

In bash, the former is simply the conditional command supporting a superset of the conditional expressions that [ supports, and the latter is a standalone command that exits with status 0 if the enclosed arithmetic expression is non-zero, or 1 otherwise. Other than readability, there is no significant difference between the two when used properly.

Jeff Schaller
  • 66,199
  • 35
  • 114
  • 250
chepner
  • 7,341
  • 1
  • 26
  • 27