50

I'm using FPM tool to create .deb package. This tool create before/after remove package from supported files.

Unfortunatly the bash script generated by FPM contains such function

dummy() {
}

And this script exit with an error:

Syntax error: "}" unexpected

Does BASH doesn't allow empty functions? Which version of bash/linux have this limitation?

user3550394
  • 713
  • 2
  • 6
  • 7
  • 1
    For why this is a syntax error, see https://stackoverflow.com/questions/39307615/why-cannot-i-define-an-empty-function-in-shell – jarmod Oct 16 '19 at 14:46

5 Answers5

66

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

dummy(){
     : 
  }
orestiss
  • 2,183
  • 2
  • 19
  • 23
  • 1
    Consider adding an example to show OP how to do this in a function. You're correct but this could be a *much* better answer with the tiniest bit more explanation – arco444 Jun 23 '15 at 09:26
  • 5
    also you can probably write `true` for readability. (I guess not everyone knows or would easily understand what `:` is.) – VasiliNovikov Feb 11 '18 at 13:27
20

A one liner

dummy(){ :; }


: is the null command

; is needed in the one line format

JobJob
  • 3,822
  • 3
  • 33
  • 34
8

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

YangwuWang
  • 116
  • 2
7

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
# ..
Damian
  • 102
  • 1
  • 8
1
dummy_success(){ true; } #always returns 0
dummy_fail(){ false; }   #always returns 1

minimal functions returning always OK or ERROR status..

also useful to redefine missing functions with empty function (or update it with some default action, for example - debug warning):

#!/bin/sh
#avoid error if calling unimportant_func which is underfined
declare -F unimportant_func >/dev/null || unimportant_func() { true; }
    
#get error if calling important_func which is underfined
declare -F important_func >/dev/null || important_func() { false; }

# print debug assert if function not exists
declare -F some_func >/dev/null || some_func() {
    echo "assert: '${FUNCNAME[0]}() is not defined. called from ${BASH_SOURCE[1]##*/}[${BASH_LINENO[0]}]:${FUNCNAME[1]}($@)" 1>&2; }


my_func(){
    echo $(some_func a1 a2 a3)
    important_func   && echo "important_func ok"   || echo "important_func error"
    unimportant_func && echo "unimportant_func ok" || echo "unimportant_func error"
}

my_func

output:

$> testlib.sh
assert: 'some_func() is not defined. called from testlib.sh[15]:my_func(a1 a2 a3)

important_func error
unimportant_func ok
Fedor
  • 31
  • 5