I have a script that exports a function for child scripts to use and would like to change what function is exported based on a conditional. I know I could just declare the two different functions within the conditional but would prefer to avoid that if possible.
So an example would be:
#!/bin/bash
foo () { echo foo; }
bar () { echo bar; }
if [[ $var == foo ]]; then
#in this case my_func() should execute foo()
export -f my_func
elif [[ $var == bar ]]; then
#in this case my_func() should execute bar()
export -f my_func
fi
I guess I could also just make a simple inception function like my_func() { foo "$@"; } inside the conditional before the export too but am asking if there is a better way to do this.
foo () { echo foo; }; eval "$(echo "my_func()"; declare -f foo | tail -n +2)"; my_func → foo(export -fcloses my terminal).