DEV Community

Discussion on: Bash If Statements: Beginner to Advanced

Collapse
 
dpshelio profile image
David Pérez-Suárez

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.

Collapse
 
rpalo profile image
Ryan Palo

Thanks! I mention this in the article, but this is a better, more complete explanation 😁