In my bash script, I execute some commands as another user. I want to call a bash function using su
.
my_function()
{
do_something
}
su username -c "my_function"
The above script doesn't work. Of course, my_function
is not defined inside su
. One idea I have is to put the function into a separate file. Do you have a better idea that avoids making another file?
You can export the function to make it available to the subshell:
export -f my_function
su username -c "my_function"