Shell Syntax: How to correctly use \ to break lines?
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.
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.