cmd1 && cmd2 && cmd3
|| operators are modelled after optimized implementation of logical operators in C.
&& means AND operator, and
|| means OR. Unix is tightly related to C, and in C, the second operand of logical operators isn't evaluated if the result is already known from the first operand. E.g.
"false && x" is
false for any
x, so there is no need to evaluate
x (especially if
x is a function call); similarly for
"true || x". This is also called short-circuiting semantics.
And in Unix, it is traditional to interpret commands' return values as "successful completion" truth values: exit code 0 means
true (success), nonzero means
false (failure). So, when the first command in
cmd1 && cmd2 returns
"false" (nonzero exit status, which indicates failure), the compound command's status is known: failure. So overall interpretation of
cmd1 && cmd2 may be: "execute
cmd1, AND THEN, if it didn't fail,
cmd2". Which is what you basically want in your question.
Similarly with OR:
cmd1 || cmd2 can be interpreted as "execute
cmd1, OR IF it fails,
Protip: for longer chains of
&&, consider putting
set -e in your script. It basically changes the semicolon
&&, with a couple of special cases.