Consider
echo \ # this is a comment
foo
This gives:
$ sh foo.sh
# this is a comment
foo.sh: line 2: foo: command not found
After some searching on the web, I found a solution by DigitalRoss on sister site Stack Overflow. So one can do
echo `: this is a comment` \
foo
or alternatively
echo $(: this is a comment) \
foo
However, DigitalRoss didn't explain why these solutions work. I'd appreciate an explanation. He replied with a comment:
There used to be a shell
gotocommand which branched to labels specified like:here. Thegotois gone but you can still use the: whateversyntax ...:is a sort of parsed comment now.
But I'd like more details and context, including a discussion of portability.
Of course, if anyone has other solutions, that would be good too.
See also the earlier question How to comment multi-line commands in shell scripts?.
Take home message from the discussion below.
The `: this is a comment` is just a command substitution.
The output of : this is a comment is nothing, and that
gets put in the place of `: this is a comment`.
A better choice is the following:
echo `# this is a comment` \
foo
make_FIND, a quickie script which builds a long list of arguments tofind. Here, the motivation for building it chunk by chunk is that each chunk comes from the body of a loop, but the same style allows to comment on each chunk. – Gilles 'SO- stop being evil' Aug 21 '11 at 17:46| # First comment before the command... More comment lines can be added up to the actual command... in bash (and Ubuntu's sh, which is actually dash) – Peter.O Aug 21 '11 at 23:50