94

I used to believe that the appropriate way of breaking the lines in a list is

command1 && \
command2

It turned out that it isn't so , one doesn't need \

$ [  $(id -u) -eq 1000  ] &&                                                   
> echo yes
yes

The same works with pipes | the same way.

The bash man page sections on pipelining and lists didn't shed any light on this. Thus , my question is : what is the proper usage of \ to break long lines ?

4
  • The example works for me; what do you get?
    – Jeff Schaller
    May 5, 2016 at 16:49
  • @JeffSchaller Sorry, I made it clearer . What I mean is that `\` is needed for some things but not for others. I just want to know the proper usage of it . And for some odd reason \ cannot be highlighted by the asterisc in comments May 5, 2016 at 16:52
  • 2
    \ is an explicit continuation; bash gives you PS2 if the command is not yet complete. Is that the confusion?
    – Jeff Schaller
    May 5, 2016 at 16:54
  • I'm not sure this is quite a duplicate, at least as asked in the title. Although the OP asks in the context of the &&, it is still general enough a more comprehensive answer could be given that is not specific to that.
    – labyrinth
    Oct 7, 2016 at 16:15

2 Answers 2

122

If the statement would be correct without continuation, you need to use \. Therefore, the following works without a backslash, as you can't end a command with a &&:

echo 1 &&
echo 2

Here, you need the backslash:

echo 1 2 3 \
4

or

echo 1 \
&& echo 2

Otherwise, bash would execute the command right after processing the first line without waiting for the next one.

2
  • 1
    So "continuation" is the term I was looking for. Bash manual explains that in the Quoting section, so I did find that . . . Now, you said one can't end a line with && and it gives PS2 prompt. What are some of the other cases ? Is there a list somewhere in the bash manual? May 5, 2016 at 17:01
  • Very clear. It's strange this is not covered in the bash manual
    – Seamus
    Apr 15, 2022 at 8:49
43

One of the scripting style guidelines I've encountered during my professional life at a huge IT company, obligated me to use no longer than 80 characters per line in a shellscript and indenting after breaking the line. Also, I had to break line before a pipe or && or ||. Like :

command1 \
  && command2 \
  || command3 \
  | command4

The goal was to have a clear readability.

5
  • 5
    I did that too, until someone pointed out that you can leave out the \ if you put the &&, ||, and | on the previous line: Even better readability.
    – Ole Tange
    May 5, 2016 at 19:59
  • 1
    @ole yup i got that, but if there is a guideline to follow, my code wouldn't pass the review if I'd do that
    – saussure
    May 5, 2016 at 21:10
  • 7
    +1. I tend to use the style mentioned by @OleTange (at least for long or complicated commands) - but the style you mention has the clear advantage that you can see what kind of continuation it is just by looking only at the line itself, you don't have to also look at the end of the previous line. Nice. I'm not sure if i like it enough to force myself to change my habits, but i like it.
    – cas
    May 6, 2016 at 2:59
  • Frankly, I prefer putting the && and || at the beginning of lines. Just like I put the OR and AND at the beginning of lines in my SQL WHERE clauses.
    – ErikE
    Aug 13, 2019 at 21:20
  • IMO, there is huge benefit in consistency. If in some cases you can MUST use ` and in other cases it is optional, I would prefer to use ` everywhere that my command continues on the next line. Scanning a script you can easily miss that a line is continued because it ends with && or || or |. (And as @ErikE says, i actually prefer to have my && and || on the next line). Oct 15, 2020 at 4:43

Not the answer you're looking for? Browse other questions tagged .