Just out of curiosity is there ever a reason to prefer something like:
while [[ $number -lt 10 ]]
over
while (( $number < 10 ))
They seem to do the same thing. I'm wondering if maybe there is a performance benefit with one of them or maybe some portability concerns with [[ ]]
?
Neither is standard, so portability is not an issue. If you are only performing simple comparisons, there is little difference betwee
if [[ $number -lt 10 ]]
and
if (( number < 10 ))
aside from the ability to drop the $
from the second (since all strings are assumed to be variables and dereferenced) and readability.
You might prefer [[...]]
when your conditional combines arithmetic and non-arithmetic tests:
if [[ $number -lt 10 && $file = *.txt ]]
vs
if (( number < 10 )) && [[ $file = *.txt ]]
You would probably prefer (( ... ))
if your comparison involved some computations:
if (( number*2 < 10-delta ))
vs the needlessly complex
if [[ $(( number*2 )) -lt $(( 10-delta )) ]]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With