Check to see if a script was run as root

If you need to make sure a script is run as root, add the following to the start of the script:

  if [[ $UID -ne 0 ]]; then
    echo "$0 must be run as root"
    exit 1
  fi

Mitch Frazier is an embedded systems programmer at Emerson Electric Co. Mitch has been a contributor to and a friend of Linux Journal since the early 2000s.

Load Disqus comments