2

I know that I can use unset -f $FUNCTION_NAME to unset a single function in bash / zsh, but how do I unset all functions?

2 Answers 2

5

In the zsh shell, you may disable all functions using

disable -f -m '*'

(literally, "disable each function whose name matches *").

You may then enable them again with the analogous enable call.

You may also use unset in a similar way to remove the functions completely from the current environment:

unset -f -m '*'
4
  • Is there a way to disable all functions whose names don't start with _, or only disable functions defined in a file? Thanks!
    – Teddy C
    Commented Mar 10, 2020 at 11:31
  • 1
    @TeddyC My answer uses * as a globbing pattern that matches all function names. You could use disable -f -m '[!_]*' to disable all functions that does not start with an underscore. This could not be used for disabling the functions defined in a file unless their names follow a specific pattern. If by "defined" you mean that their names are simply listed, one per line, in a file, you could use disable -f $(<thefile)
    – Kusalananda
    Commented Mar 10, 2020 at 11:38
  • By "defined" I mean that what the functions do are defined in the file - what about this case then? Thanks!
    – Teddy C
    Commented Mar 10, 2020 at 11:49
  • @TeddyC This is quite a different problem then. You may want to ask about this in a separate question.
    – Kusalananda
    Commented Mar 10, 2020 at 15:16
4

The associative array functions contains all the defined functions (including autoloading stubs), so ${(k)functions} expands to the list of names of defined functions.

unset -f ${(k)functions}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .