125

If you set or export an environment variable in bash, you can unset it. If you set an alias in bash, you can unalias it. But there doesn't seem to be an unfunction.

Consider this (trivial) bash function, for example, set in a .bash_aliases file and read at shell initialization.

function foo () { echo "bar" ; }

How can I clear this function definition from my current shell?
(Changing the initialization files or restarting the shell doesn't count.)

3
  • 1
    All that being said, unfunction would be a nice name for a command :-)
    – Joey
    Jun 19, 2010 at 18:00
  • @Johannes Rössel: zsh has it. Jun 19, 2010 at 20:57
  • Hm, indeed. Still, I find PowerShell's way of dealing with all this slightly more intuitive and consistent :-)
    – Joey
    Jun 20, 2010 at 2:12

3 Answers 3

162

The unset built-in command takes an option, -f, to delete functions:

unset -f foo

Form the unset entry in the bash manpage:

If -f is specified, each name refers to a shell function, and the function definition is removed.

Note: -f is only really necessary if a variable with the same name exists. If you do not also have a variable named foo, then unset foo will delete the function.

7
  • that's what i was missing. i'd found the -f parameter in export but hadn't found it in unset. thanks. Jun 19, 2010 at 3:57
  • 2
    @XavierStuvw: unset is not an external command, it is part of the shell (bash in this case); try man bash and find the entry for unset (the manage is quite large, unset is under the Shell Builtin Commands section). Mar 5, 2017 at 21:39
  • 2
    as of bash 4.3.30, -f is necessary even if no variable is named foo,
    – ychaouche
    Nov 18, 2020 at 7:51
  • 1
    It seems that if you defined a function multiple times, it cannot be unset no matter what. This has driven me crazy. Jul 25, 2021 at 13:45
  • 1
    @YanKingYin I just ran into the "cannot be unset" issue. Maddeningly, the fix seems to be start another terminal. Sep 13, 2023 at 21:14
7

See help unset:

unset: unset [-f] [-v] [-n] [name ...]

Unset values and attributes of shell variables and functions.

For each NAME, remove the corresponding variable or function.

Options:
  -f    treat each NAME as a shell function
  -v    treat each NAME as a shell variable
  -n    treat each NAME as a name reference and unset the variable itself
    rather than the variable it references

Without options, unset first tries to unset a variable, and if that fails,
tries to unset a function.

Some variables cannot be unset; also see `readonly'.

Exit Status:
Returns success unless an invalid option is given or a NAME is read-only.

There is neither unset --help nor man unset unfortunately.

6
  • 5
    There is not man unset because it is a built-in command and for the built-in commands there is instead help. For some built-in command it exists an external executable too. In case of doubt try e.g. type mycommnad. For example type unset and it will answer you unset is a shell builtin. If you want to know if it exists the executable too try which echo (another buikt-in) and it will say where it is. The --option_long is typical of the executable command. Try echo --help and /bin/echo --help. :-).
    – Hastur
    Mar 6, 2017 at 11:46
  • @Hastur Feel free to add your comment to the answer by editing its tail. If the reviewers approves of it, you'll be awarded 2 points. Mar 6, 2017 at 18:28
  • 1
    ...thanks for the thought Xavier: it was more for you than to complete the answer; I guess you missed that it was a built-in command and for them you have to use help instead of man. (Not really true because you find them in man bash in the chapter SHELL BUILTIN COMMANDS or directly with man BASH-BUILTINS...). It's cosy that under a bash shell you can simply ask for help and you will receive the list of built-in command :-). (Try help help and man man too).
    – Hastur
    Mar 7, 2017 at 14:56
  • 1
    @Hastur You may well complete the answer for the benefit of all readers. Then we can delete the comments to make good riddance of small-print clutter. Mar 7, 2017 at 15:14
  • 1
    info zsh unset or info bash unset will bring up documentation
    – Karl
    Dec 9, 2022 at 3:46
0

I recently wanted to completely uninstall nvm and reinstall it, and get rid of any of its environments. Turns out that getting rid of it does not seem to because much of nvm is in part implemented as a boatload of shell functions that are sourced into your shell via .bash_profile or .bashrc, or wherever you added those sourcing commands it told you to when you first installed it.

Baffled at first by which nvm returning nothing yet clearly the nvm command and others were still being found, I eventually discovered via declare -F that it's a bunch of shell functions. I didn't want to just kill the shell and start a new one (for reasons not relevant here), so I cleared nvm functions out of that shell with this:

for F in `declare -F | grep -e nvm | cut -f 3 -d\ `; do unset -f $F; done

Some variations on that might be helpful to someone out there that for whatever reason wants to do something similar and can't restart a new shell or doesn't want to.

You must log in to answer this question.

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