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.
I'm a Sr. Software Engineer at Flashpoint. I specialize in Python and Go, building functional, practical, and maintainable web systems leveraging Kubernetes and the cloud. Blog opinions are my own.
Great post!! Even when I've been using bash and
iffor 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
-gtcommands or the((.For example:
returns nothing, because is doing a string comparison, and
35is smaller than4(more info).Whereas, as you've shown in your post
-gtor 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 😁