Multiline comments in POSIX sh
Posted on
I realized one can use heredocs for multiline comments in sh:
…instead of:
What is going on: the language defines
<< as a redirect of the "here-document" type. After << you can put
anything, and sh will use that as a token to look for on the following lines:
Make sure to include the 'quotes' around the word, otherwise anything with a $
would get replaced just like a "double-quoted" string.
"bleh" works fine, like any word would.
But that example left as-is would print the string to stdout. To avoid that, we shove the contents to
/dev/null. So it isn’t technically a comment, its more like a constant string that gets emitted
and discarded.
Sure its hacky, but at this point what in sh isn’t? It even supports nesting!
I’ve never committed any code with this, just used while debugging sh code. Make sure to handle with care
4 messages on the fediverse
Reply on Mastodon | Lemmy