Function command execution from root

I have a function hello, that is echoing i have put that function in .bash1 file then recalling the function with same user but with su command but it is not working.

username -> test
function -> below function save in .bash1

function hello() {
   echo "Hello, $1!"
}
export -f hello

I run the below command from root

# su - test -c /home/test/.bash1 ; hello

Above command giving me error

-bash: hello: command not found

Note: What should i do that so my .bash1 function will run from root with su command, if it is not possible then what else the solution.

Exporting the function won't help, since you're calling it from its parents shell.

You will need to source the file, and quote the commands:

# su - test -c ". /home/test/.bash1; hello"
1 Like

Thanks so much, now i understand the logic.