What is the purpose of the : (colon) GNU Bash builtin?

前端 未结 12 909
梦如初夏
梦如初夏 2020-11-22 14:01

What is the purpose of a command that does nothing, being little more than a comment leader, but is actually a shell builtin in and of itself?

It\'s slower than inse

12条回答
  •  被撕碎了的回忆
    2020-11-22 14:28

    I use it to easily enable/disable variable commands:

    #!/bin/bash
    if [[ "$VERBOSE" == "" || "$VERBOSE" == "0" ]]; then
        vecho=":"     # no "verbose echo"
    else
        vecho=echo    # enable "verbose echo"
    fi
    
    $vecho "Verbose echo is ON"
    

    Thus

    $ ./vecho
    $ VERBOSE=1 ./vecho
    Verbose echo is ON
    

    This makes for a clean script. This cannot be done with '#'.

    Also,

    : >afile
    

    is one of the simplest ways to guarantee that 'afile' exists but is 0 length.

提交回复
热议问题