How to return user name in a script even if run by SUDO?

Hal Burgiss hal at burgiss.net
Tue Apr 7 01:36:49 UTC 2009


On Mon, Apr 06, 2009 at 08:57:58PM -0400, Arch Willingham wrote:
> How do I return the name of the user in a script even if it is fired off by SUDO? In other words, if my script has these two lines:
> 
> echo ${USERNAME}
> echo ${USER}
> 
> If I run it normally when logged in a user named wareagle, it prints out:
> 
> 
> wareagle
> wareagle
> 
> But if I run it via sudo, it prints out:
> 
> root
> root
> 
> I need to be able to know the name of the currently logged in user even if the script is run by sudo?

halb at honey:~$ sudo bash -c set |grep $USER
HOME=/home/halb
PWD=/home/halb
SUDO_USER=halb
XAUTHORITY=/home/halb/.Xauthority

I think that is all straight Ubunutu. Different *nixes may do things a
little different here and there so it might be hard to depend on just
like that.

-- 
Hal





More information about the ubuntu-users mailing list