Comments on the Command-Line

February 16, 2020

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:

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:

> gzip report.html # send to bob later
> history | tail
# snip
894  gzip report.html # send to bob later
895  history | tail
>

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:

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:

> # cat data.csv | awk -F, '{print $3}' | sort | uniq -c
> history | tail
# snip
896  # cat data.csv | awk -F, '{print $3}' | sort | uniq -c
897  history | tail
>

In that case, shell comments can be the git stash of your command-line session 😄

Discuss on Twitter