Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to determine whether a function exists in a POSIX shell?

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.

like image 718
Helmut Grohne Avatar asked Sep 14 '25 22:09

Helmut Grohne


1 Answers

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
}
like image 123
Mateusz Piotrowski Avatar answered Sep 17 '25 11:09

Mateusz Piotrowski