Bash: Check if user is root
Here’s a quick and easy way to ensure your script is running as root. I started using this with my server setup scripts at work to ensure they don’t fail somewhere through the script.
# Check if root
if [ "$(whoami)" != "root" ]; then
echo "Not running as root, exiting..."
echo "Please use sudo -s then run this script again."
exit 0
else
echo "Script running as root, continuing..."
fi














