Determine if a function exists in bash

后端 未结 13 2614
再見小時候
再見小時候 2020-11-30 17:39

Currently I\'m doing some unit tests which are executed from bash. Unit tests are initialized, executed and cleaned up in a bash script. This script usualy contains an init(

13条回答
  •  独厮守ぢ
    2020-11-30 18:16

    I would improve it to:

    fn_exists()
    {
        type $1 2>/dev/null | grep -q 'is a function'
    }
    

    And use it like this:

    fn_exists test_function
    if [ $? -eq 0 ]; then
        echo 'Function exists!'
    else
        echo 'Function does not exist...'
    fi
    

提交回复
热议问题