Way to create multiline comments in Bash?

后端 未结 8 534
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-04 04:59

I have recently started studying shell script and I\'d like to be able to comment out a set of lines in a shell script. I mean like it is in case of C/Java :



        
8条回答
  •  一生所求
    2020-12-04 05:08

    Here's how I do multiline comments in bash.

    This mechanism has two advantages that I appreciate. One is that comments can be nested. The other is that blocks can be enabled by simply commenting out the initiating line.

    #!/bin/bash
    # : <<'####.block.A'
    echo "foo {" 1>&2
    fn data1
    echo "foo }" 1>&2
    : <<'####.block.B'
    fn data2 || exit
    exit 1
    ####.block.B
    echo "can't happen" 1>&2
    ####.block.A
    

    In the example above the "B" block is commented out, but the parts of the "A" block that are not the "B" block are not commented out.

    Running that example will produce this output:

    foo {
    ./example: line 5: fn: command not found
    foo }
    can't happen
    

提交回复
热议问题