Great post!! Even when I've been using bash and if for very long time, your post have helped me to put in the right place certains things I thought I understood.
I need to add something that's bitten me a few times, when comparing numbers, be sure either to use -gt commands or the ((.
For example:
$ if[[ 35 > 4 ]];then echo"great";fi
returns nothing, because is doing a string comparison, and 35 is smaller than 4 (more info).
Whereas, as you've shown in your post -gt or using the (( works as expected.
$ if[[ 35 -gt 4 ]];then echo"great";fi
great
Another bit I missed here is the short circuit evaluations. But I always spent more time trying to get them right than just writing three extra lines.
Ryan is an engineer in the Sacramento Area with a focus in Python, Ruby, and Rust. Bash/Python Exercism mentor. Coding, physics, calculus, music, woodworking. Looking for work!
Great post!! Even when I've been using bash and
if
for very long time, your post have helped me to put in the right place certains things I thought I understood.I need to add something that's bitten me a few times, when comparing numbers, be sure either to use
-gt
commands or the((
.For example:
returns nothing, because is doing a string comparison, and
35
is smaller than4
(more info).Whereas, as you've shown in your post
-gt
or using the((
works as expected.Another bit I missed here is the short circuit evaluations. But I always spent more time trying to get them right than just writing three extra lines.
Thanks! I mention this in the article, but this is a better, more complete explanation 😁