How can I execute a bash function using sudo?

Each time you run sudo, it forks and execs a new copy of the shell, running as root. That shell does not inherit functions from your shell (it can’t) and it doesn’t inherit functions from previous executions. You will have to write out a file containing the function definition and invocation and sudo the invocation of that.

Leave a Comment