A Neat POSIX Shell Trick

Shell formatters and human reviewers alike hate this one simple trick...

It's this:

for i do
  echo "${i}"
done

You are reading that right. Where the heck does i come from? Well, in POSIX Shell, looping over a variable without specifying what you're looping over simply defaults to "${@}": all arguments!

I've found not many people know the above! But I've also found that humans and robots alike often don't know that in this situation you can also omit the ; before the do, and without a new line. I am not entirely sure why but believe it is to do with the fact that in a normal for loop:

for i in "${@}"; do

The variable gets "expanded" similarly to this:

for i in "apple" "banana" "coconut"; do

The shell parser would ordinarily need to know where the list of items to iterate over ends and the loop body begins. But with the weird "iterate over arguments by default" feature, the list we're iterating over is assumed, and as it's not stated it need not be accounted for with a ;.

Completely valid. Completely cursed. And I (personally) love it!