Reputation: 5781
How do I get the current user's username in Bash? Do I use whoami
?
Upvotes: 564
Views: 777422
Reputation: 1
If you want to get the fullname and not the user id, you can get from
/etc/passwd and get your line with grep
; or simpler with id -P
which get the line for the current user; and, in all case, something like that:
id -P|cut -d":" -f5,5|cut -d"," -f1,1
(first cut to get the GECOS part, then 2nd cut to get the fullname, in case other info is present)
Upvotes: 0
Reputation: 171
This is a small simple example bash script I made for pushing my code to my personal gitlab, it spits out my current username in my commit message.
# !/bin/sh
# This example script is for pushing my code to gitlab
echo Starting Push for user : $(whoami), Please enter Commit Message
below:
read varMessage
# this prompts the user for an input messsage , then saves the result in
# a variable
git add .
git commit -m "$(whoami): $varMessage"
git push -u "url_of_project" master
Resultant commit message in my personal gitlab looks like this:-
walexia : updated Matplotib example
Upvotes: 1
Reputation: 10396
REALUSER="${SUDO_USER:-${USER}}"
...gets you the regular user (if non-sudo) → or ← the regular user behind the current sudo call.
Upvotes: 8
Reputation: 156
All,
From what I'm seeing here all answers are wrong, especially if you entered the sudo mode, with all returning 'root' instead of the logged in user. The answer is in using 'who' and finding eh 'tty1' user and extracting that. Thw "w" command works the same and var=$SUDO_USER gets the real logged in user.
Cheers!
TBNK
Upvotes: 0
Reputation: 788
On most Linux systems, simply typing whoami on the command line provides the user ID.
However, on Solaris, you may have to determine the user ID, by determining the UID of the user logged-in through the command below.
echo $UID
Once the UID is known, find the user by matching the UID against the /etc/passwd
file.
cat /etc/passwd | cut -d":" -f1,3
Upvotes: 3
Reputation: 71
In Solaris OS I used this command:
$ who am i # Remember to use it with space.
On Linux- Someone already answered this in comments.
$ whoami # Without space
Upvotes: 7
Reputation: 901
Use the standard Unix/Linux/BSD/MacOS command logname
to retrieve the logged in user. This ignores the environment as well as sudo, as these are unreliable reporters. It will always print the logged in user's name and then exit. This command has been around since about 1981.
My-Mac:~ devin$ logname
devin
My-Mac:~ devin$ sudo logname
Password:
devin
My-Mac:~ devin$ sudo su -
My-Mac:~ root# logname
devin
My-Mac:~ root# echo $USER
root
Upvotes: 49
Reputation: 156
When root (sudo) permissions are required, which is usually 90%+ when using scripts, the methods in previous answers always give you root
as the answer.
To get the current "logged in" user is just as simple, but it requires accessing different variables: $SUDO_UID
and $SUDO_USER
.
They can be echoed:
echo $SUDO_UID
echo $SUDO_USER
Or assigned, for example:
myuid=$SUDO_UID
myuname=$SUDO_USER
Upvotes: 13
Reputation: 291
A hack the I've used on Solaris 9 and Linux and which works fine for both of them:
ps -o user= -p $$ | awk '{print $1}'
This snippet prints the name of the user with the current EUID.
NOTE: you need Bash as the interpreter here.
On Solaris you have problems with methods, described above:
id
does not accept the -u
and -n
parameters (so you will have to parse the output)whoami
does not exist (by default)who am I
prints owner of current terminal (ignores EUID)$USER
variable is set correctly only after reading profile files (for example, /etc/profile
)Upvotes: 22
Reputation: 6020
An alternative to whoami
is id -u -n
.
id -u
will return the user id (e.g. 0 for root).
Upvotes: 143
Reputation: 2161
For Bash, KornShell (ksh
), sh
, etc. Many of your questions are quickly answered by either:
man [function]
to get the documentation for the system you are using or usually more conveniently:
google "man function"
This may give different results for some things where Linux and Unix have modest differences.
For this question, just enter "whoami" in your shell.
To script it:
myvar=$(whoami)
Upvotes: 4
Reputation: 81
The current user's username can be gotten in pure Bash with the ${parameter@operator}
parameter expansion (introduced in Bash 4.4):
$ : \\u
$ printf '%s\n' "${_@P}"
The :
built-in (synonym of true
) is used instead of a temporary variable by setting the last argument, which is stored in $_
. We then expand it (\u
) as if it were a prompt string with the P
operator.
This is better than using $USER
, as $USER
is just a regular environmental variable; it can be modified, unset, etc. Even if it isn't intentionally tampered with, a common case where it's still incorrect is when the user is switched without starting a login shell (su
's default).
Upvotes: 6
Reputation: 322
Get the current task's user_struct
#define get_current_user() \
({ \
struct user_struct *__u; \
const struct cred *__cred; \
__cred = current_cred(); \
__u = get_uid(__cred->user); \
__u; \
})
Upvotes: -5
Reputation: 13726
Two commands:
id
prints the user id along with the groups.
Format: uid=usernumber(username) ...
whoami
gives the current user name
Upvotes: 12