Require sudo in bash script

Use the following code snippet in bash scripts that require root privileges to prompt the user to become root using sudo.

if [ "$EUID" -ne 0 ]; then
    echo "This script requires root."
    sudo su -s "$0"
    exit
fi