Inline comments for Bash?

BashComments

Bash Problem Overview


I'd like to be able to comment out a single flag in a one-line command. Bash only seems to have from # till end-of-line comments. I'm looking at tricks like:

ls -l $([ ] && -F is turned off) -a /etc

It's ugly, but better than nothing. Is there a better way?

The following seems to work, but I'm not sure whether it is portable:

ls -l `# -F is turned off` -a /etc

Bash Solutions


Solution 1 - Bash

My preferred is:

https://stackoverflow.com/questions/1455988/commenting-in-bash-script#answer-1456019

> This will have some overhead, but technically it does answer your question > > echo abc #put your comment here
> def #another chance for a comment
> xyz etc > > And for pipelines specifically, there is a cleaner solution with no overhead > > echo abc | # normal comment OK here > tr a-z A-Z | # another normal comment OK here > sort | # the pipelines are automatically continued > uniq # final comment > > https://stackoverflow.com/q/9522631/#12797512

Solution 2 - Bash

I find it easiest (and most readable) to just copy the line and comment out the original version:

#Old version of ls:
#ls -l $([ ] && -F is turned off) -a /etc
ls -l -a /etc

Solution 3 - Bash

$(: ...) is a little less ugly, but still not good.

Solution 4 - Bash

Here's my solution for inline comments in between multiple piped commands.

Example uncommented code:

    #!/bin/sh
    cat input.txt \
    | grep something \
    | sort -r

Solution for a pipe comment (using a helper function):

    #!/bin/sh
    pipe_comment() {
        cat - 
    }
    cat input.txt \
    | pipe_comment "filter down to lines that contain the word: something" \
    | grep something \
    | pipe_comment "reverse sort what is left" \
    | sort -r

Or if you prefer, here's the same solution without the helper function, but it's a little messier:

    #!/bin/sh
    cat input.txt \
    | cat - `: filter down to lines that contain the word: something` \
    | grep something \
    | cat - `: reverse sort what is left` \
    | sort -r

Solution 5 - Bash

Most commands allow args to come in any order. Just move the commented flags to the end of the line:

ls -l -a /etc # -F is turned off

Then to turn it back on, just uncomment and remove the text:

ls -l -a /etc -F

Solution 6 - Bash

How about storing it in a variable?

#extraargs=-F
ls -l $extraargs -a /etc

Solution 7 - Bash

If you know a variable is empty, you could use it as a comment. Of course if it is not empty it will mess up your command.

ls -l ${1# -F is turned off} -a /etc

§ 10.2. Parameter Substitution

Solution 8 - Bash

For disabling a part of a command like a && b, I simply created an empty script x which is on path, so I can do things like:

mvn install && runProject

when I need to build, and

x mvn install && runProject

when not (using Ctrl + A and Ctrl + E to move to the beginning and end).

As noted in comments, another way to do that is Bash built-in : instead of x:

$  : Hello world, how are you? && echo "Fine."
Fine.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionLajos NagyView Question on Stackoverflow
Solution 1 - BashRafareinoView Answer on Stackoverflow
Solution 2 - BashDanView Answer on Stackoverflow
Solution 3 - BashIgnacio Vazquez-AbramsView Answer on Stackoverflow
Solution 4 - BashKylePDavisView Answer on Stackoverflow
Solution 5 - Bashleedm777View Answer on Stackoverflow
Solution 6 - BashKaroly HorvathView Answer on Stackoverflow
Solution 7 - BashZomboView Answer on Stackoverflow
Solution 8 - BashOndra ŽižkaView Answer on Stackoverflow