How do I rename a bash function?
bash, function
Solution
Here's a way to eliminate the temp file:
$ theirfunc() { echo "do their thing"; }
$ eval "$(echo "orig_theirfunc()"; declare -f theirfunc | tail -n +2)"
$ theirfunc() { echo "do my thing"; orig_theirfunc; }
$ theirfunc
do my thing
do their thing
Problem
I am developing some convenience wrappers around another software package that defines a bash function. I would like to replace their bash function with an identically-named function of my own, while still being able to run their function from within mine. In other words, I need to either rename their function, or create some kind of persistent alias to it that won't be modified when I create my function of the same name. To give a brief example of a naive attempt that I didn't expect to work (and indeed it does not): ``` $ theirfunc() { echo "do their thing"; } $ _orig_theirfunc() { theirfunc; } $ theirfunc() { echo "do my thing"; _orig_theirfunc } $ theirfunc do my thing do my thing do my thing ... ``` Obviously I don't want infinite recursion, I want: ``` do my thing do their thing ``` How can I do this?