Bash: Split Long Lines
1 min readSep 2, 2022
Sometimes a statement is so long that it exceeds a reasonable line length (perhaps 80 characters as recommended by some style guides).
cmd | cmd | cmd | cmd
When a line exceeds the number of characters you decided to use in your scripts, or when it becomes hard to read, that’s when you should probably split your line into multiple lines like this:
cmd \
| cmd \
| cmd \
| cmd
You use \
for splitting. There are some other recommendations that make such a split more readable:
- add a two-space (or whatever your script already uses) indent at the beginning of each line
- put
|
right after the indent
All this also goes for ||
and &&
:
cmd \
&& cmd \
|| cmd
You can even use the same principle with a command that has many options:
cmd \
--option-one \
--option-two param \
--option-three
I’d argue that this is much more readable than cramming all the options on one line.