0

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

NicoESIEA
  • 499
  • 1
  • 6
  • 23
  • Sure, I can easily use an IF condition between each command: command 1 if [ $? == 0 ] then commande 2 if [ $? == 0 ] then commande 3 .... But it is not as beautiful as using && ... :( – NicoESIEA Jan 14 '19 at 09:33
  • 4
    Just put `&&` on the end of the line. – KamilCuk Jan 14 '19 at 09:34
  • 1
    `&&` on the beginning of the line makes it easier to visually parse a multi-line statement. A backslash will allow the statement to continue past a line break. – Attie Jan 14 '19 at 09:37
  • If it’s all about stopping when something fails then `set -e` would do. – Biffen Jan 14 '19 at 09:37
  • @Biffen - it's not... – Attie Jan 14 '19 at 09:39
  • 3
    Welcome to StackOverflow. Please do not post answers in your question. Instead, please remove the answer part of your question and post an answer to your own question. This is the intended way. Thank you and have a great time around here. – Micha Wiedenmann Jan 14 '19 at 10:50
  • Leave the answers to the answers section. – Pound Hash Oct 10 '22 at 21:45

2 Answers2

8

Bash knows that && requires for there to be another command, so put it before the line break to avoid backslashes.

command 1 &&
command 2 &&
command 3 ... etc

The question sounds vaguely like you should be using something else instead, though. If not set -e then perhaps make instead of a plain old shell script?

tripleee
  • 175,061
  • 34
  • 275
  • 318
3

Backslash!

command 1 \
 && command 2 \
 && command 3 \
 && command 4 \
 && command 5 \
 && command 6 \
 && command 7 \
 && command 8 \
...
 && command n

Just watch out and don't mistakenly put a backslash on the last line!

Attie
  • 6,690
  • 2
  • 24
  • 34
iBug
  • 35,554
  • 7
  • 89
  • 134