How do I delete/remove a shell function?

BashShellZsh

Bash Problem Overview


I have done this:

$ z() { echo 'hello world'; }

How do I get rid of it?

Bash Solutions


Solution 1 - Bash

unset -f z

Will unset the function named z. A couple people have answered with:

unset z

but if you have a function and a variable named z only the variable will be unset, not the function.

Solution 2 - Bash

In Zsh:

unfunction z

That's another (arguably better) name for unhash -f z or unset -f z and is consistent with the rest of the family of:

  • unset
  • unhash
  • unalias
  • unlimit
  • unsetopt

When in doubt with such things, type un<tab> to see the complete list.

(Slightly related: It's also nice to have functions/aliases like realiases, refunctions, resetopts, reenv, etc to "re-source" respective files, if you've separated/grouped them as such.)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questiontoo much phpView Question on Stackoverflow
Solution 1 - BashRobert GambleView Answer on Stackoverflow
Solution 2 - BashMicah ElliottView Answer on Stackoverflow