`whoami` equivalent env variable 

Newsgroups: comp.unix.shell
> I used to use $LOGNAME or $USER to avoid one layer of system call of
> `whoami`. Later I found out that in some systems or shells, they
> don't exist, so I backed off to use `whoami`.
>
> Now I'm thinking to re-use the variable approach again...
>
> I want to pick a generally-available-to-all-shell variable and
> stick to it, accross all shells

IIRC, login sets LOGNAME. Which shell you use won't effect whether or not login sets LOGNAME.

Derek M. Flynn

`whoami` equivalent env variable 

from man login:

The basic environment is initialized to:
HOME=your-login-directory
LOGNAME=your-login-name
PATH=/usr/bin:
SHELL=last-field-of-passwd-entry
MAIL=/var/mail/your-login-name
TZ=timezone-specification

`whoami` equivalent env variable 

>Which shell you use won't effect whether or not
>login sets LOGNAME.

However, programs may be invoked without going through login. For instance, login isn't involved if you run a program via rsh or cron.

Barry Margolin

`whoami` equivalent env variable 

>I used to use $LOGNAME or $USER to avoid one layer of system call of
>`whoami`. Later I found out that in some systems or shells, they
>don't exist, so I backed off to use `whoami`.
>
>Now I'm thinking to re-use the variable approach again. can somebody
>give me some general ideas like "originally where they are defined",
>"which shell have them", etc.

$LOGNAME is the POSIX-blessed spelling of this variable; it's origins are in the AT&T System V (and even some if its predecessors) world. $USER, however, has at least as many adherents; it's orgins are the BSD 4.0 (and perhaps even older) releases.

>I want to pick a generally-available-to-all-shell variable and
>stick to it, accross all shells, providing the default value if the
>shell itself doesn't define it.

Despite its flaws, I tend to favor following POSIX whenever making an arbritrary choice between a set of actions, so I'd suggest using $LOGNAME as your convention. You can initialize it with:

: ${LOGNAME=${USER=`whoami`}}

("If $LOGNAME is not defined, set it to the value of $USER, which, in its turn, is set to `whoami` if it is also not already defined.")

Ken Pizzini

documented on: 2000.08.07 Mon 22:30:32