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?)
Best Answer
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.