Search This Blog

Shell Script: check if the current user is root

The system environment variable $EUID can be used. root's EUID is 0.

#!/bin/bash


if [[ $EUID -ne 0 ]]; then
    echo "I am not root."
else
    echo "I am root."
fi


NOTE: EUID is not available in standard POSIX shell /bin/sh. You will have to use /bin/bash if you want to use EUID in your script. See also this article.

No comments:

Post a Comment