Several good answers here. Always take style guides with a grain of salt, and IMHO and experience, be ever-so-slightly-to-moderately concerned about any community that is rife with excessive dogma when it comes to style. It's almost like they believe that when they FINALLY get that last I dotted, and that last T crossed, then the software will work. Sometimes it takes more than perfect style to remove bugs...
When I started learning shell, most scripts and tutes out there used the in-line semicolon format
for i in "$@"; do
stuff
done
but because I was inexperienced, I had a bit of a hard time spotting the ; and do - both essential for confirming syntactical correctness. So I preferred the do on the next line all by itself. I no longer do that (pun intended)
Further, the 'while' command is an excellent candidate for a nice little trick:
while prep1; prep2; condition; do
stuff
done
but when the prep commands are a bit bulky, and/or the condition is complex, it is better formatted as
while
prep1
prep2
condition
do
stuff
done
and then appending the do as "; do" is positively fugly.
You can even get more intense than that:
while
if con1; then
cond2
elif cond3; then
cond4
elif cond5; then
cond6
else
cond7
fi
do
stuff
done
because every statement is an expression. Notice how both styles are used opportunistically. Keep it clean and it's quite readable. Compact or contort it in the name of style or "saving space" and it becomes a horrible mess.
So! Given the rather baroque stylings of shell scripts in general, anything aimed at increasing clarity and easy visual access to significant symbols is always a good thing.
The form where 'do' is written inline with the command is sweet when you have a little command - I don't find the 'do' visually hidden, nor is the inner command really obscured:
for i in whatever
do stuff
done
I find that quite pleasant to look at, and it has analogues with while, if and case:
while condition
do stuff
end
if condition
then stuff1
else stuff2
fi
case $blah in
a) stuff1;;
b) stuff2;;
c) stuff3;;
*) stuffInfinity;;
esac
Clarity and general tidiness is all that Codd* asks of you.
*Edgar F. Codd, father of relational database theory.
do
is definitely a bit unusual, butfor ... \ndo\n<indent>body...
is extremely common and I'd even estimate more so thanfor ... ; do
. Does it also complain about that?bashate
is a style checker. Styles are inherently subjective. Don't use it if you don't like the artibrary style it imposes. You should instead consider a syntax/bug checker like shellcheck.do
. It's like indenting the opening brace of anif
and adding code on that same line in a language like C. Another example, if python allowed it, would be putting the:
of anif
indented on the next line and adding code on that same line. It's going to make it differ with additional lines in the body.