Comments in a multi-line bash command


This single-command BASH script file is difficult to understand, so I want to write a comment for each of the actions:

echo 'foo'     \
| sed 's/d/a/' \
| sed 's/e/b/' \
| sed 's/f/c/' \
> myfile

(sed is just an example, actually it is a mix of greps and trs and awks)

I would hate having to duplicate lines, or having each comment far away from the line it applies to.
But at the same time BASH does not seem to allow "in-line" comments.

Any elegant way to solve this problem?

Best Answer

Put the pipes at the end of line with the comments after it:

$ echo 'foo' |
sed 's/f/a/' | # change first f to a
sed 's/o/b/' | # change first o to b
sed 's/o/c/'   # change second o to c
Related Question