Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Also, will these variables always match currently logged-in username (they do on my Debian system)? Can I assume their availability in other Unix(-like) systems?

I'm also curious why one would use whoami instead of just reading any of these variables.

share|improve this question
Looking at the man page, whoami reports the name associated with your effective user ID. Which means it will return something different if you're using sudo or running a setuid executable. If you have sudo set up, try sudo whoami for example. – Joseph R. May 19 '13 at 11:44
USER and USERNAME are ordinary environment variables, which means that, if you want, you can set them to arbitrary values. Just type USER=xyz. In other words, even if those variables exist, there is no guarantee that their values match the currently logged-in username. – Uwe May 19 '13 at 12:03
@Uwe By guarantee, I meant by default (i.e. assuming user did not change them). – Tshepang May 19 '13 at 12:22
@Tshepang As a follow up to my first comment: compare the results of sudo whoami and sudo echo $USER – Joseph R. May 19 '13 at 16:58
@JosephR. For sudo echo $USER, the shell expands $USER, then calls sudo. So of course it doesn't produce the same output as whoami. Like sudo whoami, sudo sh -c 'echo $USER' does (typically) output root. Regarding your comment about whoami using the EUID, note that sudo whoami would output root even if whoami used the UID. sudo sets both EUID and UID for the command it runs (except in the very unusual situation that you explicitly configure it to behave otherwise). Compare sudo id -u to sudo id -ru. – Eliah Kagan Oct 7 '14 at 16:55

It's login.

The Linux login(1) man page says:

The value for $HOME, $USER, $SHELL, $PATH, $LOGNAME, and $MAIL are set according to the appropriate fields in the password entry.

The FreeBSD login(1) man page says:

The login utility enters information into the environment (see environ(7)) specifying the user's home directory (HOME), command interpreter (SHELL), search path (PATH), terminal type (TERM) and user name (both LOGNAME and USER).

The NetBSD, OpenBSD and OS X man pages say the same thing.

Here's the source code from the util-linux login:

setenv("HOME", pwd->pw_dir, 0); /* legal to override */
setenv("USER", pwd->pw_name, 1);
setenv("SHELL", pwd->pw_shell, 1);
/* ... */
setenv("LOGNAME", pwd->pw_name, 1);

Here's the source code from the FreeBSD login:

(void)setenv("LOGNAME", username, 1);
(void)setenv("USER", username, 1);
(void)setenv("PATH", rootlogin ? _PATH_STDPATH : _PATH_DEFPATH, 0);
share|improve this answer
On my Fedora 16 box, I have both USER and USERNAME set and your command only returns LOGNAME. – Joseph R. May 19 '13 at 11:50
@JosephR., unfortunately I don't have Fedora on hands but I've looked into FreeBSD's sources as well, see UPD.. – poige May 19 '13 at 12:43
But this is obviously not the case on Fedora. All I'm saying is, login doesn't seem to be the only thing setting these variables. – Joseph R. May 19 '13 at 14:10

There's no rule. Some shells like tcsh or zsh set $LOGNAME. zsh sets $USER.

It may be set by some things like login, cron, su, rshd, graphical login managers or may not.

If there's been a login though, in my experience, $USER is generally set (but it may not be updated after a change of user id (via setuid commands) within that login session. POSIX requires that $LOGNAME be set upon login (and cron).

To get the login name portably, best is to use the logname command (if there's not been any login, it may return nothing). To get the user id, use id -u. To get one username corresponding to the current effective user id: id -un. To get all of them:

perl -le 'while ($n = getpwent()) {print $n if getpwnam($n) == $>}'
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.