Check if shell-script is runing with root permissions (or with sudo)

Some scripts need to run as root (iptables for temporary rules, installer scripts, …). A quick way to check if the shell-script is running as root or with sudo (sudo should be the preferred way) is as follows.

if [ ! ${EUID} -eq 0 ]; then
  echo "Please run as root or with sudo"
  exit
fi

this has been tested tested on debian-based and arch-linux, but should also work on most other linux derivates.

it’s not new - but i was not aware of it until recently.


comments powered by Disqus