Comments on the Command-Line
Bulgarian Translation, thanks to Zlatan.
Most shells support comments with the #
symbol. At first glance, that’s
obvious because shell commands can be turned into shell scripts … and all
programming languages have comments.
But comments are more complicated than they seem; their uses vary:
- documentation
- explanations
- compiler annotations, sometimes
- debugging – commenting out code
- tagging – TODO, FIXME, etc…
What makes shell comments different?
Shell comments are part of the shell’s history!
If you type a command and add a trailing comment, it’s going to history
:
I was surprised: that’s not how I imagined the shell would behave (by default).
And though that’s a cute use of shell comments, it’s not necessarily something I would recommend. I wouldn’t build a workflow around this…
How to leverage this?
Personally, I use this feature when:
- I’m in the middle of typing a command
- something else happens, I need to pause this…
- but I don’t want to lose my “work”
It might be because I need to consult a man page, or because I need to do something else first. In those cases, I go to the beginning of the line (ctrl-a) and comment out the whole thing:
In that case, shell comments can be the git stash
of your command-line session 😄