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

Smoot Carl-Mitchell smoot at
Wed Apr 8 12:45:49 UTC 2009

On Tue, 2009-04-07 at 22:55 -0700, Sundar Nagarajan wrote:

> why not just use ${SUDO_USER:-$USER}
> will return ${SUDO_USER} if set (when inside a sudo shell) and return 
> ${USER} otherwise (not inside a sudo shell).

That will work as well.  I had forgotten that particular shell syntax.
Good catch!
Smoot Carl-Mitchell
Computer Systems and
Network Consultant
smoot at
+1 480 922 7313
cell: +1 602 421 9005

More information about the ubuntu-users mailing list