Will Clarke

Force a script to run as root (sudo)

2020-07-16

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" "$@"; exit $?; }

Tags

unix bash