26

I have seen this construct in scripts a lot and used it myself, but it bothers me that I can't seem to find it in the documentation.

Example:

[ -f file1 ] &&
[ -f file2 ] &&
echo "Both files exist." ||
echo "One or the other file doesn't exist."

This could also be done with backslashes before the newlines, as mentioned in man bash:

If a \<newline> pair appears,  and  the  backslash  is  not
itself  quoted,  the \<newline> is treated as a line continuation (that
is, it is removed from the input stream and effectively ignored).

Example:

[ -f file1 ] && \
[ -f file2 ] && \
echo "Both files exist." || \
echo "One or the other file doesn't exist."

...but this doesn't seem to be necessary. The first version above works even without the backslashes.

Where can I find this in man bash? (Also, is this bash specific or POSIX compliant?)

  • If you’re looking for official bash or POSIX documentation, see Gilles’s answer.   But this is discussed in What are the shell's control and redirection operators?More observations on ;, &, ( and ). – G-Man Aug 12 '16 at 5:57
  • @G-Man, I guess that wasn't directed at me? Just for future readers? As I said in the question, "it bothers me that I can't seem to find it in the documentation." I already knew how it works, so this was indeed just a request for the official documentation. :) – Wildcard Aug 12 '16 at 6:00
  • Even in the case of arrays, there is an implicit line continuation: names=( Rama Soma<newline> Sita Diya ). Python clearly describes these here but the Bash documentation doesn't seem to. – jamadagni Jun 1 '18 at 4:01
38

A newline is ignored in a few contexts where there is manifestly an unterminated command. These contexts include after a control operator (&&, ||, |, &, ;, ;;, but not !).

I don't see this documented in the bash manual.

In POSIX, it's specified via the grammar rules. Wherever the rules have linebreak, you can have zero or more line breaks.

  • +1, I didn't knew it worked for the simple pipe ( | ) . I wish we could also put them on the next line, for readability, but it doesn't work that way (if doing that, you need to espace the preceding newline). – Olivier Dulac Jan 6 '16 at 12:49
  • 1
    @OlivierDulac How could that possibly work? When it's processing the current command line, how is it supposed to know that you're planning to type | at the beginning of the next line? Remember, the shell is also used interactively, not just in scripts, and the grammar is the same. – Barmar Jan 6 '16 at 19:43
  • @Barmar : I know, I do know this (wrote compilers at school ^^)... I said "I wish", and it is not a very plausible wish – Olivier Dulac Jan 6 '16 at 19:54
  • 1
    @DocSalvager, can you give an example of a logic bug that would help prevent? I have a hard time seeing it.... – Wildcard Jan 9 '16 at 5:34
  • 2
    @DocSalvager, I wouldn't actually use the code as I wrote it—it was an illustration. Without the last line (the else clause) I would use it that way, though. (For "if this and this then do that" this && this2 && that is fine but for an else clause I'd use an actual if then else fi.) – Wildcard Jan 13 '16 at 4:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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