How do I unset or get rid of a bash function?
Rise to the top 3% as a developer or hire one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------
Music by Eric Matyas
https://www.soundimage.org
Track title: Romantic Lands Beckon
--
Chapters
00:00 How Do I Unset Or Get Rid Of A Bash Function?
00:36 Accepted Answer Score 168
01:06 Answer 2 Score 8
01:34 Answer 3 Score 0
02:26 Thank you
--
Full question
https://superuser.com/questions/154332/h...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#linux #commandline #bash #shell
#avk47
ACCEPTED ANSWER
Score 168
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.
ANSWER 2
Score 8
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.
ANSWER 3
Score 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.