Shell Syntax: How to correctly use to break lines?

I used to believe that the appropriate way of breaking the lines in a list is

command1 && 
command2

It turned out that it isn’t so , one doesn’t need

$ [  $(id -u) -eq 1000  ] &&                                                   
> echo yes
yes

The same works with pipes | the same way.

The bash man page sections on pipelining and lists didn’t shed any light on this. Thus , my question is : what is the proper usage of to break long lines ?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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.

Method 2

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.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x