3 ms·
Regardless of `-a` vs two tests and `&&`, there's no need to shell out to `expr` if bash's arithmetic evaluation is available: [ $((1+1)) -eq 2 ]
by darrenf 3y ago
Regardless of `-a` vs two tests and `&&`, there's no need to shell out to `expr` if bash's arithmetic evaluation is available:
[ $((1+1)) -eq 2 ]
- kqr 3y agoI'm going to assume that if one is using [ rather than [[ then one will also want to use expr rather than $(()).
- ykonstant 3y agoArithmetic expansion is a feature of all POSIX compliant shells, which furthermore advises against using `expr`. The latter is only needed for non-compliant shells like some legacy implementations of the Bourne shell.
- kqr 3y agoOh, I did not know that! Thanks.
- darrenf 3y agoWhat's the basis for that assumption? I'm struggling to see a reason to not use the shell for arithmetic, regardless of `/usr/bin/[`, builtin `[` or `[[`.
- neuromanser 3y agoarithmetic evaluation is ((…)), $((…)) is arithmetic expansion. There’s no need for test(1) / [(1) or conditional expressions ([[…]]) if you’re doing arithmetic: if ((1+1 == 2)); then …; fi