DEV Community

Roy
Roy

Posted on

cmd1 && cmd2 || cmd3 is not the same as if cmd1; then cmd2; else cmd3;fi

http://mywiki.wooledge.org/BashPitfalls#pf22

Top comments (2)

Collapse
 
thefluxapex profile image
Ian Pride

That's a superb reference page, by the way; appreciate the link. I do know most of what's there, but it's always awesome to have compilations of useful use-case's and fixes of common caveats/pitfalls.

Collapse
 
grscheller profile image
Geoffrey R. Scheller

$ [[ x == x ]] || [[ x == y ]] && [[ x == y ]]
$ echo $?
1

$ [[ x == x || x == y && x == y ]]
$ echo $?
0

Outside of [[ ]], && and || have equal pressidence.