Difference between EUID and UID?
Solution 1
It only works on bash, not in dash (in Debian based distros as Ubuntu sh is usually a symlink to dash).
If you are running the script interactively you might not have bash configured as your default shell, run bash
before trying.
If you are running it from console:
bash script.sh
If you are running it using its path (for example ./script.sh
) ensure the first line of the script is:
#!/bin/bash
And not:
#!/bin/sh
For a more generic way to do it check: https://askubuntu.com/questions/15853/how-can-a-script-check-if-its-being-run-as-root
In that post the command id
is mentioned, where:
id -u # is the EUID
id -u -r # is the UID
Solution 2
They're different when a program is running set-uid. Effective UID is the user you changed to, UID is the original user.
Related videos on Youtube
Arun Chettoor
Updated on March 06, 2020Comments
-
Arun Chettoor about 4 years
EUID is not the same as UID. At what context are these both are used in the script?
I tried to get the values by
echo "UID is $UID and EUID is $EUID"
, but only space came as output. My machine runs Ubuntu 12.04 LTS. Seen at some sites that this is usually used to check whether it is root user and all but not able to get proper difference.-
Arun Chettoor over 9 yearsA link to know how to change uid and gid.
usermod -u <NEWUID> <LOGIN> groupmod -g <NEWGID> <GROUP> find / -user <OLDUID> -exec chown -h <NEWUID> {} \; find / -group <OLDGID> -exec chgrp -h <NEWGID> {} \; usermod -g <NEWGID> <LOGIN>
-
-
Arun Chettoor over 9 yearsSo why my current shell not displaying any integer. Could you please explain a little bit more!
-
Barmar over 9 yearsI don't know. It works for me on OS X running bash 3.2.48 and Debian running bash 4.1.5.
-
Arun Chettoor over 9 yearsWhen I am running inside a script it is not coming, but when i write it directly on the shell it is working!
-
Barmar over 9 yearsWorks for me both ways.
-
Arun Chettoor over 9 yearsOK. May be I should get a guy who runs Ubuntu 12.04 lts to check it!
-
Matt almost 6 yearswhich one only works on bash ?? EUID or UID ? you're not clear
-
ikwyl6 about 4 yearsThe subject is "Difference between EUID and UID?" but you just tell OP how to get uid or euid or when it works (in bash). Don't know why this is the preferred answer..
-
SebMa over 3 years@Matt I wanted to edit the answer of Fernando but the "Suggested edit queue is full". Both
EUID
andUID
variables are only available inbash
andzsh