Is it possible to save the old function (named foo
) in such a way that you can call it within a new function (also named foo
), perhaps from a different name, or such?
To make this concrete, here's specifically what I'm trying to do:
In bash, the command_not_found_handle
is a function which, if defined, is called whenever bash cannot find the command which the user is trying to run. For example, Ubuntu uses this to suggest packages which can be installed:
$ pv
The program 'pv' is currently not installed. You can install it by typing:
sudo apt-get install pv
This is really nice, and I'd like to keep this behavior. However, I'd also like to add another command_not_found
handle, which simply runs a given DEFAULT_COMMAND
with whatever command line the user typed, like so:
$ DEFAULT_COMMAND=git
$ status
On branch master
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working
directory)
modified: .bashrc
modified: .gitconfig
modified: .vimrc
Untracked files:
(use "git add <file>..." to include in what will be committed)
.bash_profile
.gitglobalignore
.gvimrc
node_modules/
no changes added to commit (use "git add" and/or "git commit -a")
I already have a function which does the above, and it works like a dream; however, using it overrides the default Ubuntu command_not_found
handle, which can be handy to have when I can't remember a package name for a given executable.
How do I get both? Thanks!
NB: The obvious solution is to find and copy the Ubuntu built-in command_not_found
logic into my script; but that is not ideal, because it means I have to manually update it later, when Ubuntu changes the way they do things. I'm hoping for something more generic, if possible.
Edit: It would be best not to have to resort to string manipulation, as can be done by saving the text of the function to a variable, mangling it, then eval
ing it.