Empty function in BASH

Solution 1:

You could use : that is equivalent to true and is mostly used as do nothing operator...

dummy(){
     : 
  }

Solution 2:

A one liner

dummy(){ :; }


: is the null command

; is needed in the one line format

Solution 3:

An empty bash function may be illegal. function contains only comments will be considered to be empty too.

a ":" (null command) can be placed in function if you want to "DO NOTHING"

see: http://tldp.org/LDP/abs/html/functions.html

Solution 4:

I recommend this one:

dummy(){ unused(){ :;} }


If you use : null command, it will be printed by xtrace option:

(
    set -o xtrace
    dummy(){ :; }
    dummy "null command"
)

echo ------

(
    set -o xtrace
    dummy(){ unused(){ :;} }
    dummy "unused function"
)

output:

+ dummy 'null command'
+ :
------
+ dummy 'unused function'

For debug I use wrapper like this:

main() {(
    pwd # doing something in subshell
)}

print_and_run() {
    clear
    (
        eval "$1() { unused() { :; } }"
        set -o xtrace
        "$@"        
    )
    time "$@"
}

print_and_run main aaa "bb bb" ccc "ddd"
# output:
# + main aaa 'bb bb' ccc ddd
# ..