Bash: Execute a Function as Sudo within a Script
From WikiMLT
Probably the simplest way is the follow:
#!/bin/bash
# Define the function
main() { ... }
sudo -b bash -c "$(declare -f main); main"
References:
- Auto login – LughtDM – SSH
- Display terminal command result in Desktop notification
- Executing a Bash Script Function with Sudo
- How can I execute a bash function using sudo?