This is basically the same question as Determine if a function exists in bash, except that this time it's not aiming at Bash, but at a POSIX shell:
How to determine whether a shell function with a given name exists?
It seems that none of the typical built-ins like type
are mandated by POSIX, so the matter is more difficult or maybe even impossible.
For the sake of completeness: it is possible to use type
or command -V
without spawning any extra subprocesses like sed
or grep
(although you still have to spawn one for $(type ...)
):
is_function() {
case "$(type -- "$1" 2>/dev/null)" in
*function*) return 0 ;;
esac
return 1
}
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