Inline comments for Bash?

前端 未结 8 929
深忆病人
深忆病人 2020-12-07 13:28

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:<

8条回答
  •  暖寄归人
    2020-12-07 14:00

    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
    

提交回复
热议问题