What is the use case of noop [:] in bash?

后端 未结 12 915
滥情空心
滥情空心 2020-12-12 13:07

I searched for noop in bash (:), but was not able to find any good information. What is the exact purpose or use case of this operator?

I tried following and it\'s w

相关标签:
12条回答
  • 2020-12-12 13:19

    Two of mine.

    Embed POD comments

    A quite funky application of : is for embedding POD comments in bash scripts, so that man pages can be quickly generated. Of course, one would eventually rewrite the whole script in Perl ;-)

    Run-time function binding

    This is a sort of code pattern for binding functions at run-time. F.i., have a debugging function to do something only if a certain flag is set:

    #!/bin/bash
    # noop-demo.sh 
    shopt -s expand_aliases
    
    dbg=${DBG:-''}
    
    function _log_dbg {
        echo >&2 "[DBG] $@"
    }
    
    log_dbg_hook=':'
    
    [ "$dbg" ] && log_dbg_hook='_log_dbg'
    
    alias log_dbg=$log_dbg_hook
    
    
    echo "Testing noop alias..."
    log_dbg 'foo' 'bar'
    

    You get:

    $ ./noop-demo.sh 
    Testing noop alias...
    $ DBG=1 ./noop-demo.sh 
    Testing noop alias...
    [DBG] foo bar
    
    0 讨论(0)
  • 2020-12-12 13:20

    Sometimes no-op clauses can make your code more readable.

    That can be a matter of opinion, but here's an example. Let's suppose you've created a function that works by taking two unix paths. It calculates the 'change path' needed to cd from one path to another. You place a restriction on your function that the paths must both start with a '/' OR both must not.

    function chgpath() {
        # toC, fromC are the first characters of the argument paths.
        if [[ "$toC" == / && "$fromC" == / ]] || [[ "$toC" != / && "$fromC" != / ]]
        then
            true      # continue with function
        else
            return 1  # Skip function.
        fi
    

    Some developers will want to remove the no-op but that would mean negating the conditional:

    function chgpath() {
        # toC, fromC are the first characters of the argument paths.
        if [[ "$toC" != / || "$fromC" == / ]] && [[ "$toC" == / || "$fromC" != / ]]
        then
            return 1  # Skip function.
        fi
    

    Now -in my opinion- its not so clear from the if-clause the conditions in which you'd want to skip doing the function. To eliminate the no-op and do it clearly, you would want to move the if-clause out of the function:

        if [[ "$toC" == / && "$fromC" == / ]] || [[ "$toC" != / && "$fromC" != / ]]
        then
            cdPath=$(chgPath pathA pathB)   # (we moved the conditional outside)
    

    That looks better, but many times we can't do this; we want the check to be done inside the function.

    So how often does this happen? Not very often. Maybe once or twice a year. It happens often enough, that you should be aware of it. I don't shy away from using it when I think it improves the readability of my code (regardless of the language).

    0 讨论(0)
  • 2020-12-12 13:28

    If you use set- e then || : is a great way to not exit the script if a failure happens (it explicitly makes it pass).

    0 讨论(0)
  • 2020-12-12 13:32

    Ignore alias arguments

    Some times you want to have an alias that doesn't take any argument. You can do it using ::

    > alias alert_with_args='echo hello there'
    
    > alias alert='echo hello there;:'
    
    > alert_with_args blabla
    hello there blabla
    
    > alert blabla
    hello there
    
    0 讨论(0)
  • 2020-12-12 13:35

    I've also used in it scripts to define default variables.

    
    : ${VARIABLE1:=my_default_value}
    : ${VARIABLE2:=other_default_value}
    call-my-script ${VARIABLE1} ${VARIABLE2}
    
    
    0 讨论(0)
  • 2020-12-12 13:37

    I sometimes use it on Docker files to keep RUN commands aligned, as in:

    RUN : \
        && somecommand1 \
        && somecommand2 \
        && somecommand3
    

    For me, it reads better than:

    RUN somecommand1 \
        && somecommand2 \
        && somecommand3
    

    But this is just a matter of preference, of course

    0 讨论(0)
提交回复
热议问题