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

command1 && \

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

$ [  $(id -u) -eq 1000  ] &&                                                   
> echo 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 ?

  • The example works for me; what do you get? – Jeff Schaller May 5 '16 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 – Sergiy Kolodyazhnyy May 5 '16 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 '16 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 '16 at 16:15

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 \


echo 1 \
&& echo 2

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

| improve this answer | |
  • 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? – Sergiy Kolodyazhnyy May 5 '16 at 17:01

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.

| improve this answer | |
  • 4
    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 '16 at 19:59
  • @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 '16 at 21:10
  • 5
    +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 '16 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 '19 at 21:20

Not the answer you're looking for? Browse other questions tagged or ask your own question.