I have a bash script that will only work with root privileges, so I want to test whether the user has them. Other posts (see below) ask and answer how to know whether the user is actually running as root
, but not whether the script has root privileges. These posts say to test whether $EUID
is 0.
To try this idea in the context of sudo
, I wrote a bash script /tmp/a.sh
:
#!/bin/bash
echo $EUID
The following two commands were run as a non-root user with sudo
privileges on Ubuntu 16.04. If the $EUID
suggestion worked in the context of sudo
, the second command would have printed 0 instead of a blank line.
$ /tmp/a.sh
1000
$ sudo /tmp/a.sh
$
FYI, an example of the related posts I am referencing is:
echo $UID
should work. Or according to David F in the comments of your linked answer you should useecho $(id -u)
$EUID
doesn't work, please edit the question to clarify the title and the body of the question, and we can re-open it