Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

When creating shell scripts using pipelines, and using the backslash to continue lines, I want to insert comments on separate lines, in a robust, readable and portable fashion.

For example, given this uncommented multi-line command (stolen from @DigitalRoss for its clarity):

echo abc |
     tr a-z A-Z |
     sort |

... the below is aesthetically closest to what I want to accomplish, but for obvious reasons, does not work ... and yes, I'm well aware that this isn't something normally worth commenting:

# Perform critical system task.
# NOTE - An example of what does *not* work.
echo abc |
    # Convert lowercase to uppercase.
     tr a-z A-Z |

     # Sort the results.
     sort |

     # Only show unique lines.

Existing related answers appear unsatisfactory for me, as follows:

First, glenn jackman's answer (adding arguments to an array, and then executing the array) works for single commands, but does not work for pipelining (and even if it did, it adds complexity that I would like to avoid).

Second, @Gilles' answer here (which uses :) also doesn't appear to work with pipelining, because it alters the flow of the pipeline:

$ echo "abc" | :

(NOTE: If there is an equivalent to : that passes output unmodified, that would be aesthetically acceptable, but I haven't been able to find one. I could write a custom one, but it would reduce portability.)

Finally, the last part of DigitalRoss' answer on StackOverflow works well for adding comments on the same line, but I strongly prefer comments on separate lines. Otherwise, when lines have widely varying lengths, readability is reduced:

echo abc |         # normal comment OK here`
     /usr/local/bin/really/long/path/ridiculously-long-filename.sh |  # another normal comment OK here
     sort |        # the pipelines are automatically continued
     uniq          # final comment

I'm looking for answers that preserve readability and minimize complexity, or else some background on why what I'm looking for is infeasible.

share|improve this question
Note about your comments: I was taught to write comments explaining a section or unit of work. If an experienced coder writes intelligent comments describing what they're doing, then presumably anyone coming along maintaining the script can always use man pages if they don't understand the details of use. One should never write a comment like i++; // increment i by one. I suggest you read "The Elements of Programming Style" – bsd Dec 13 '11 at 17:57
I think I'm going to delete the last edit. It's unusual for a question to contain the question, refutations of answers, and the answer itself, but the part that explains why other answers don't work is kind of necessary here. rozcietrzewiacz's answer and your comment together explain the problem though (personally I would've posted a separate answer and accepted it, since the problem was fairly different from what he described, but this works) – Michael Mrozek Dec 14 '11 at 14:46
Understood - thanks for the feedback; lesson learned! – Royce Williams Dec 14 '11 at 15:03
up vote 9 down vote accepted

How about this?

echo abc | \
# Convert lowercase to uppercase.
tr a-z A-Z | \

# Sort the results.
sort | \

# Only show unique lines.

(The backslash needs to be used as the last character in those lines.) I'm not sure about the portability of that approach, but it sure works with current bash.

share|improve this answer
Aha! (facepalm) I transformed the idiom I was actually using when posting the question without realizing it! I was putting the pipe at the beginning of each line, which I am happy to stop doing now that I know that this method works well on all of the systems I can reach, in both bash and sh. Thanks! – Royce Williams Dec 14 '11 at 0:28

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.