My bash script sources a script file (call it
file2.sh) according to an argument. (It is either sourced or not.) The script
file2.sh contains a function "foo" (call it a modified or improved version of foo).
The script also sources another file (
file1.sh) which contains the original function "foo". This one (
file1.sh) is always sourced (and it has other functions that are required).
I need the function "foo" in
file2.sh to override the "foo" in file1.sh (if
file2.sh is sourced).
Furthermore, I need to do this in any scripts that my main script calls. Several of those called script files also source
file1.sh. They are expecting the original "foo" function. But I need to make them call the improved "foo" without modifying them.
In other words, if my main script includes
file2.sh, I want any script (which is called by my main script) that sources
file1.sh to use "foo" from
file2.sh instead. And I can't change any of the other files except my main script. (Or if I do change them, I need them to work correctly when
file2.sh is not sourced by the main script.)