POSIX compatible systems have LOGNAME environment variable set whose value will equal to current user name:
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):
There are also some commands available:
will print current user name.
Id utilities can also print user name:
This prints the same name as whoami command.
Yet another command:
who am i