POSIX compatible systems have LOGNAME environment variable set whose value will equal to current user name:

echo $LOGNAME

This is the most portable way to detect current user name.

Quite common on Linux there is, besides LOGNAME, USER environment variable, whose value equals to $LOGNAME (most of the time):

echo $USER

There are also some commands available:

whoami

will print current user name.

Id utilities can also print user name:

id -un

This prints the same name as whoami command.

Yet another command:

who am i

Leave a Reply