Bash: Execute a Function as Sudo within a Script

From WikiMLT

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: