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?

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.

  • 1
    (( )) and $(( )) would perhaps make it easier to mix other arithmetic in, if that's ever needed? – ilkkachu Oct 4 '16 at 21:22
  • Is [ available in any shell? If there would be a shell where [ is not built-in then $(( )) could be significant faster. – rudimeier Oct 4 '16 at 22:30
  • 1
    @rudimeier In theory [ can be external but in practice it's always built in. – Gilles 'SO- stop being evil' Oct 5 '16 at 0:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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