Force a script to run as root (sudo)

Just chuck this in at the start of a bash script and it’ll ensure the rest of the script is run by the root user:

#!/usr/bin/env sh
[ `whoami` = root ] || { sudo "$0" "[email protected]"; exit $?; }
comments powered by Disqus