Consecutive conditions 

Synopsis 

do not use the form

condition_yes && condition_no || echo ok

Use

condition_yes && ! condition_no && echo ok

Analysis 

Conditions can be evaluated consecutively.

For example, if I want to do 'echo ok' based on two consecutive conditions, i.e, only condition 1 and condition 2 are true, I'll 'echo ok', I'll write

condition1 && condition2 && echo ok

The evaluation is shortcut, i.e, if condition1 fails, condition2 won't be executed:

$ set -x
$ true && true && echo ok
+ true
+ true
+ echo ok
ok
$ false && true && echo ok
+ false
$ true && false && echo ok
+ true
+ false

Caution, the algorithm is different than C when there is || in the evaluation:

$ true && false || echo ok
+ true
+ false
+ echo ok
ok

Seems to be working fine, but watch this:

$ false && true || echo ok
+ false
+ echo ok
ok

Oops, that's not what you intented!

documented on: 2004.05.18