[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: $USER vs. $LOGNAME and the EnvironmentVariables wiki page



On Mon, Mar 25, 2024 at 08:48:04PM +0100, Patrice Duroux wrote:
> Hi,
> 
> 1. Using CodeSearch, it is not clear to me when to use one or the other.

Your original Subject: header mentions $USER and $LOGNAME so I assume
you're asking about these.

$LOGNAME is the standard variable which is set by the login(1) program.
Every process in a standard login session should have access to this.
For other login sessions that don't use login(1), the rules may differ,
but see below.

Additionally, crontab(5) documents that cron sets LOGNAME, so all of
your cron jobs should also have access to it.

$USER is the old BSD variable which serves the same purpose.  Most of
the subsystems in Linux (including logins performed by ssh(1) and by
Display Managers) will set both LOGNAME *and* USER, in order to provide
the expected working environment for users from various backgrounds
(and to programs written by developers from various backgrounds).

If you're writing a new program today and wondering which one to use,
$LOGNAME should be the way to go.  Unless you want your program to run
on every old Unix/BSD system in the world -- then you should check for
both of them, LOGNAME and then USER, and use whichever one you find first.


Reply to: