How to Check if the script is run by root at the start of the script

Check root at start of script

Some scripts need to be run as root and you may want to check at the start of that script that it is running as root. This can be done by checking the environment variable $EUID. This variable will hold the value 0 if it’s being run as root.

#!/bin/bash
if [ "$EUID" -ne 0 ];then
    echo "Please run this script as root"
    exit 1
fi
Related Post