I am using &&
to chain commands (execute a command only if the previous command returned ok (0) code).
But I need to add this condition on many commands (more than 8) and then my code looks very ugly (too long for one unique line):
command 1 && command 2 && command 3 && command 4 && command 5 && command 6 && command 7 && command 8...
Is there a way to use &&
with more than only one line?
command 1
&& command 2
&& command 3
&& command 4
&& command 5
&& command 6
&& command 7
&& command 8
...
ANSWER
As proposed by @Kamil Cuk, using a && at the end of each line can solve my issue. But only the lines following the && will be skipped. I prefer to skip all the rest. But I agree - this proposition solve the issue. We can use && at the end of each line to apply the condition on multi line. Thank you.
I tested the proposal from @iBug. I observed the same issue, the others instructions (out of the list) are still processed. But even worst the result using \ is wrong: It executes the first command, then it prints the command 2 name...
echo "A" \
echo "B" \
echo "YES"
--> A echo B echo YES
Finally, the solution I used is found by @Biffen: set -e
-e Exit immediately if a simple command exits with a non-zero status, unless the command that fails is part of an until or while loop, part of an if statement, part of a && or || list, or if the command's return status is being inverted using !. -o errexit
Thank all of you for you quick help.
Regards, Nicolas