echo \ # this is a comment foo
$ 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
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 goto command which branched to labels specified like : here. The goto is gone but you can still use the : whatever syntax ... : 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?.