Bash: Split Long Lines

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:

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 \

I’d argue that this is much more readable than cramming all the options on one line.



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store