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

Posted on July 22, 2010 at 5:10 pm by Brad · Permalink
In: Bash, Linux · Tagged with: ,

Leave a Reply

Spam Protection by WP-SpamFree