Bash: Execute a Function as Sudo within a Script

From WikiMLT
Revision as of 12:06, 3 August 2022 by Spas (talk | contribs) (Spas moved page Bash Execute a Function as Sudo within a Script to Bash: Execute a Function as Sudo within a Script without leaving a redirect)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Prob­a­bly the sim­plest way is the fol­low:

#!/bin/bash

# Define the function
main() { ... }

sudo -b bash -c "$(declare -f main); main"

Ref­er­ences: