38

This question already has an answer here:

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 ?

marked as duplicate by Wildcard, Community May 5 '16 at 19:56

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 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
48

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.

  • 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
18

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.

  • 3
    No longer than 80 lines? Seriously? Did you mean columns? – Alexander Batischev May 5 '16 at 19:17
  • 1
    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
  • 80 characters, sorry. corrected lines to characters – saussure May 5 '16 at 21:07
  • @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
  • 3
    +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

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