The Computer Oracle

Comments in a multi-line bash command

--------------------------------------------------
Rise to the top 3% as a developer or hire one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------

Music by Eric Matyas
https://www.soundimage.org
Track title: Hypnotic Puzzle3

--

Chapters
00:00 Comments In A Multi-Line Bash Command
00:36 Accepted Answer Score 70
00:47 Answer 2 Score 19
01:23 Answer 3 Score 20
01:37 Thank you

--

Full question
https://superuser.com/questions/238791/c...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#bash #pipe #comments #sh

#avk47



ACCEPTED ANSWER

Score 70


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
abc



ANSWER 2

Score 20


I prefer this way. It allows you to write multi-line comments using command grouping

echo 'foo' | {
  # change first f to a
  # you can add more lines of comment on the command options
  sed 's/f/a/'
} | {
  # change first o to b
  sed 's/o/b/'
} | {
  # change second o to c
  sed 's/o/c/' 
}



ANSWER 3

Score 19


If you happen upon this question while looking to comment a non-pipeline multiline command:

$ echo 'foo' |
sed -e 's/f/a/' `: # change first f to a` \
    -e 's/o/b/' `: # change first o to b` \
    -e 's/o/c/' `: # change second o to c`

Unless you're doing something really perverse like automating commenting, I can't see a reason to prefer this over Mikel's answer for a pipe, but if you really wanted to:

$ 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`

or:

$ 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`

Source: http://unix.derkeiler.com/Newsgroups/comp.unix.solaris/2005-07/0991.html