Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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
2  
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
2  
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
2  
@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
1  
@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 at 16:55

2 Answers 2

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

I think it's login:

$ strings `which login`|fgrep $'LOGNAME\nUSER'
USER
LOGNAME
USERDEL_CMD
USERGROUPS_ENAB

And BTW I don't have USERNAME set on Ubuntu 13.04.

UPDATE: We can refer to FreeBSD login code, for e. g., and it explicitly sets both USER and LOGNAME. But not USERNAME:

(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
1  
@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

Your Answer

 
discard

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.