I have a shell script with two functions. The second function needs to be run as another user from a new bash shell. The first function is invoked into the second one. Function 1 uses some of the script variables.
#!/bin/bash
foo=bar
function1() {
echo -e "$1"
}
function2() {
echo "This is function2"
function1 "I use function1"
echo "I also need to print ${foo}"
}
export -f function2
su username -c "/bin/bash -c function2"
Actually this run function2 inside a new shell then fail to find neither function1 nor foo.
Is there a way to inject or pass them to the new function ?