The title says it all. What command I need to run from a terminal to find my user ID (UID)?
4 Answers
There are a couple of ways:
Using the id command you can get the real and effective user and group IDs.
id -u <username>
If no username is supplied to
id
, it will default to the current user.Using the shell variable. (It is not an environment variable, and thus is not available in
env
).echo $UID
-
8
-
26
-
It's worth noting that, due to the fact that the variables are resolved before being passed to a command, we have that
sudo echo ${UID}
prints out1000
(or whatever your sudoer user's UID is), whereassudo id -u
prints out0
. Dec 19, 2018 at 20:33 -
The
username
is optional, defaulting to yourself. Maybe square brackets would be better for indicating this, instead of angle brackets. May 13, 2019 at 15:44 -
2The second part of this answer is wrong. The variable in question is explicitly not an environment variable. It's a shell variable. Big difference. You can see this with
echo $UID
versusenv|grep ^UID
in Bash, for example. This means in particular that the first method is more robust and the second will only work in shell scripts, not - say - in something like Python (python -c 'import os; print(os.environ)'
to see the environment). Dec 16, 2020 at 15:39
Simply try
id
This will return your user ID, group ID, and all your groups.
-
15
-
-
4
Try also :
getent passwd username
This will display user id , group id and home directory .
Or:
grep username /etc/passwd
-
why to try long or alternative command while
echo $UID
andid -u
is simple and exact according to question?– PandyaMay 17, 2014 at 13:37 -
7
echo $UID
?