Ubuntu – Difference between $LOGNAME and logname

environment-variables

When the echo $LOGNAME and logname commands run normally, I get identical results from them:

pandya@pandya-desktop:~$ echo $LOGNAME
pandya
pandya@pandya-desktop:~$ logname
pandya

Is there any difference between them?

Best Answer

From Environment variables:

$LOGNAME is same as $USER which gives

The name of the currently logged-in user. This variable is set by the system. You probably shouldn't change its value manually.

From man logname:

logname - print user´s login name


Explained differently used by following example:

pandya@pandya-desktop:~$ sudo su
root@pandya-desktop:/home/pandya# echo $LOGNAME
root
root@pandya-desktop:/home/pandya# logname
pandya
root@pandya-desktop:/home/pandya# exit
exit
pandya@pandya-desktop:~$

Here you can see the difference after logging as root in a terminal,

  • $LOGNAME gives the name of the currently logged-in user in the terminal (i.e root)
  • Whereas logname prints the user's login name who logged in to the session (i.e. pandya)