I'm currently building a program which adds to the current user's shell depending on the project he's working on, by defining per-project aliases and functions. These aliases and functions may and will certainly have the same name like for instance cdproj
, which would cd
to the project's root.
I would like to remove previously defined aliases and functions when changing project (before (re)defining aliases and functions for the other project. I know I can remove an alias with unalias
in both bash and zsh, but how would I do the same for a function?
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.
Each variable or function specified by name shall be unset. If -v is specified, name refers to a variable name and the shell shall unset it and remove it from the environment. Read-only variables cannot be unset. If -f is specified, name refers to a function and the shell shall unset the function definition.
Function names and definitions may be listed with the -f option to the declare builtin command (see Bash Builtins). The -F option to declare will list the function names only (and optionally the source file and line number).
unset -f my_function
will remove (or unset) the function my_function
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With