I want to check if a function exist or not before executing it in the shell script.
Does script shell support that? and how to do it?
As read in this comment, this should make it:
type -t function_name
this returns function
if it is a function.
$ type -t f_test
$
$ f_test () { echo "hello"; }
$ type -t f_test
function
Note that type
provides good informations:
$ type -t ls
alias
$ type -t echo
builtin
POSIX does not specify any arguments for the type
built-in and leaves its output unspecified. Your best bet, apart from a shell-specific solution, is probably
if type foo | grep -i function > /dev/null; then
# foo is a function
fi
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With