Ryan Palo Avatar

Ryan Palo

Ryan Paloassertnotmagic.com

Short circuiting in Bash

Learn from Ryan Palo about the super-powers of the “short-circuit” policy in Bash.

In Bash, the && and || (AND and OR) operators work via a “short-circuit” policy.

if [[ "$1" -gt 5 ]] && [[ "$1" -lt 10 ]]; then

This checks the first condition. If the first condition is true, then there’s a possibility the whole thing could be true, so it checks the second condition. If the second condition is true, then the whole thing is true! However, if the first condition is false, then there’s no reason to check the second condition, because the whole thing could never possibly be true with the first one false.

0:00 / 0:00