Determine if a function exists in bash

后端 未结 13 2641
再見小時候
再見小時候 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 17:55

    I think you're looking for the 'type' command. It'll tell you whether something is a function, built-in function, external command, or just not defined. Example:

    $ LC_ALL=C type foo
    bash: type: foo: not found
    
    $ LC_ALL=C type ls
    ls is aliased to `ls --color=auto'
    
    $ which type
    
    $ LC_ALL=C type type
    type is a shell builtin
    
    $ LC_ALL=C type -t rvm
    function
    
    $ if [ -n "$(LC_ALL=C type -t rvm)" ] && [ "$(LC_ALL=C type -t rvm)" = function ]; then echo rvm is a function; else echo rvm is NOT a function; fi
    rvm is a function
    

提交回复
热议问题