Linux – How to unset or get rid of a bash function

bashcommand linelinuxshell

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.)

Best Answer

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.

Related Question