Difference between EUID and UID?

38,908

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.

Share:
38,908

Related videos on Youtube

Arun Chettoor
Author by

Arun Chettoor

Updated on March 06, 2020

Comments

  • Arun Chettoor
    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
      Arun Chettoor over 9 years
      A 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
    Arun Chettoor over 9 years
    So why my current shell not displaying any integer. Could you please explain a little bit more!
  • Barmar
    Barmar over 9 years
    I don't know. It works for me on OS X running bash 3.2.48 and Debian running bash 4.1.5.
  • Arun Chettoor
    Arun Chettoor over 9 years
    When I am running inside a script it is not coming, but when i write it directly on the shell it is working!
  • Barmar
    Barmar over 9 years
    Works for me both ways.
  • Arun Chettoor
    Arun Chettoor over 9 years
    OK. May be I should get a guy who runs Ubuntu 12.04 lts to check it!
  • Matt
    Matt almost 6 years
    which one only works on bash ?? EUID or UID ? you're not clear
  • ikwyl6
    ikwyl6 about 4 years
    The 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
    SebMa over 3 years
    @Matt I wanted to edit the answer of Fernando but the "Suggested edit queue is full". Both EUID and UID variables are only available in bash and zsh