1

I saw this example

$ whoami
MatsP
$ g++ -Wall -std=c++0x getenv.cpp 
$ ./a.out
MatsP
$ export USER=DonaldDuck
$ ./a.out
DonaldDuck

but for me it doesn't work. I am in a Debian machine. I used export -p and indeed the variable changed. I refreshed the bachrc, still nothing. How to achiece the above? whoami displays the default value.

traveller
  • 111
  • 2

1 Answers1

1

whoami on Linux does not read the USER variable.

On a "regular system" (meaning not using for example LDAP) it just does a geteuid(2) system call to get your current effective userid and then reads /etc/passwd to look up your username.

heemayl
  • 54,820
  • 8
  • 124
  • 141
tlund
  • 384
  • 2
  • 8
  • Ok, but why on my system the code from the link above doesn't work? I used `getenv("USER")` and saw my username and then tried to change it using `export USER=aName` and next time i run `getenv("USER")` it gives the original value. – traveller Apr 05 '16 at 13:30