I apologize in advance if this is a duplicate question. I did make an effort to search/check before asking here.
I'm comfortable with writing one-liners like this:
foocommand && foocommand2 && foocommand3
The idea being that I only want subsequent commands to run if the previous one was "successful".
I'm writing a somewhat lengthy script and this one-liner isn't feasible because it looks like a huge block of confusing code to everyone else.
I want to space out the commands and write comments inbetween them in the script. How can I do this and still have the equivalent of && in there?
&&
doesn't mean the subsequent command will run if the previous one was successful. It means the command will run if the collective result of all the previous commands in the command list is success. You may know this, but future readers may misunderstand.||
and&&
(as opposed to|
or&
) is called short circuiting. The behavior used with the latter operators is called eager evaluation.a && b && c
will only runb
ifa
succeeds, so "it only runsc
ifb
succeeds" and "it only runsc
ifa
andb
both succeed" are equivalent statements:b
can't succeed unlessa
succeeded.a || b && c
is more illustrative.true || false && echo hi
will outputhi
. The specification reads, The operators "&&" and "||" shall have equal precedence and shall be evaluated with left associativity.