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?
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?
From Environment variables:
$LOGNAME
is same as$USER
which givesFrom
man logname
:Explained differently used by following example:
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.eroot
)logname
prints the user's login name who logged in to the session (i.e.pandya
)The utility logname is broken in 16.04, apparently on purpose because it's possible to hack the value it returns, which could be a security flaw. https://bugzilla.gnome.org/show_bug.cgi?id=747046 I had been using logname in some scripts, and found it useful because it always returned the same value (my login name) whether I was calling it from from a my own level or from a sudo invocation, whereas the environment variables $USER and $LOGNAME do not. I wasn't worried about someone hacking into my computer and modifying the value it returns. Alas.... I have found a workaround. I created a file in my home directory as such
When I want to access my logname (e.g., assigning to a Bash variable g_logname), I do so thusly:
This works for me, whether as myself or at the root level through sudo, which maintains the assignment of "~" as my home directory. I can create a .logname file for the home directory of each user on the system. Yes, someone could hack into my computer and change these files, but I'm not really worried about that. I just want my scripts to work.