Using the following you can check if you are root or not when you run bash scripts.
if [ $(whoami) != 'root' ]; then
echo "Must be root to run $0"
ROOT_UID=0 # Only users with $UID 0 have root privileges.
E_NOTROOT=87 # Non-root exit error.
if [ "$UID" -ne "$ROOT_UID" ]
echo "Must be root to run this script!"
echo "You are root!"
You can also use this in your bash scripts.