I actually don't know of any shell that sets that variable.
zsh sets $USERNAME and $LOGNAME. tcsh sets $user but I think
that's all. Now, login or xdm may set the LOGNAME or USER
variables, but those are modifiables.
So, depending on what you want, the POSIX ways would be:
Login name (as written in utmp, so only in login sessions, while
the shell still has a controlling terminal):
user=$(logname)
Real user name:
user=$(id -urn)
Effective user name:
user=$(id -un)
--
Stephane
If you are paranoid about security, maybe "whoami" is better.
I think that `whoami` is supported on more shells than $(whoami).
If you are really paranoid, consider that somebody might put a fake
"whoami" somewhere in the path and better refer to the command by
absolute path :^)
Yours,
Laurenz Albe
And then, he may use LD_LIBRARY_PATH or LD_PRELOAD (or BASH_ENV
or ~/.zshenv). But he may as well edit and modify a copy of the
script, it's a bit pointless to restrict the usage of a script
(except if the script is setuid, but then you're already in big
trouble if you write setuid shell scripts).
--
Stephane
Actually there are more applications setting $LOGNAME (at
least on debian) than $USER:
$USER $LOGNAME
xdm y y
gdm y y
login y y
in.telnetd (ssl) y y
cron n y
sshd n y # note login is called
# for interactive sessions
lshd y y
in.rshd (redone) y n
in.rlogind (redone) n n # but login then does
xterm n y
zsh n y
bash n n
pdksh n n
ksh93 n n
tcsh n n
csh n n
rc n n
es n n
--
Stephane
On Solaris and RH-Linux, I find that sh, bash, csh, tcsh do set $USER...
so almost all shells have $USER.
--
Michael Tosch @ hp : com
What test did you make that shows that?
$ env -i /bin/bash -c 'echo "$USER $LOGNAME"'
$
I'd bet $USER was already in the environment when you started
bash. (set by login when you logged in for instance).
--
Stephane
You are right, I had tested /bin/sh and /bin/bash via 'rsh host echo $USER' but
USER seems to be set from rshd.
But csh and tcsh really set $USER.
$USER is expanded by the local shell, there, not the one
launched by rshd on the remote host.
>
> But csh and tcsh really set $USER.
[...]
You're half right, my affirmation was based on a test that is
only relevant on Bourne-like shells (and that I initially did to
find out which variables csh would provide with the user name):
env -u LOGNAME -u USER csh -c 'set | grep -a chazelas'
If tcsh sets $USER and $LOGNAME, csh (at least the one shipped
with debian) does not (I'd bet your csh is tcsh).
$ env -u LOGNAME -u USER tcsh -c 'env | grep -a chazelas'
[...]
LOGNAME=chazelas
USER=chazelas
GROUP=chazelas
$ env -u LOGNAME -u USER csh -c 'env | grep -a chazelas'
PWD=/home/chazelas
$
--
Stéphane
The csh(1) shipped with Solaris (9, if it's relevant) does set $USER,
but not $LOGNAME:
$ uname -srv
SunOS 5.9 Generic_117171-15
$ env -i csh -c 'env'
PWD=/home/cet1
USER=cet1
$ env -i tcsh -c 'env'
HOSTTYPE=sun4
VENDOR=sun
OSTYPE=solaris
MACHTYPE=sparc
SHLVL=1
PWD=/home/cet1
LOGNAME=cet1
USER=cet1
GROUP=cet1
HOST=*suppressed*.cam.ac.uk
and the Bourne-type shells (sh, ksh, bash) indeed set neither.
Chris Thompson
Email: cet1 [at] cam.ac.uk
Are you sure it's not set in your ~/.cshrc (remember that even
non-interactive csh read ~/.cshrc unless you pass it the "-f"
option).
--
Stéphane
If a person su's to a different userid, they probably wouldn't take the
time to change
any environment variables. I could set an environment variable to
anything I wanted
if I thought it might get me more privileges (e.g., setenv USER root).
Bill Seivert
sei...@pcisys.net
In my final solution I opted to call whoami from /usr/ucb/whoami just
in case somebody had put a different whoami on the path.
And what prevents a user to make a copy of your script and
change that path or anything else?
--
Stéphane
Yup: no .cshrc in my home directory on that system (I don't usually use csh!).
But I repeated the experiment with -f just in case ... same results.
It seems Solaris /bin/csh only sets $USER if it's not already set:
$ env -i USER=foobar PWD=/ csh -f -c 'env'
USER=foobar
PWD=/home/cet1