Stick with this article to learn the difference between the logname and whoami commands, the usage of the logname command, and how to use it in shell scripts.
|Root or Sudo Privileges||No|
|Host System and Architecture||Ubuntu 22.10 (x64)|
|OS Compatibility||Ubuntu, Manjaro, Fedora, etc.|
Syntax of the Logname Commands
The logname command takes only one argument as an option (optional).
$ logname [OPTION]
The following is the list of all known options accepted by this command.
|Display the help section.|
|Display version information.|
Usage of the Logname Command in the Terminal
This command can be directly executed without specifying any options to get the currently logged-in username.
As you can see, the usage is pretty simple, but you should also know the few differences between this and the whoami command.
Difference Between the Logname and Whoami Commands
Most of the time, both commands work the same, but when both are run with sudo privilege, the logname command only shows the user’s name, while the whoami command shows the username that is actually in charge, which will be root.
Executing both the commands without sudo privilege.
$ logname $ whoami
Executing both the commands with sudo privilege.
$ sudo logname $ sudo whoami
Furthermore, if you have multiple user accounts on your system and use the su command to switch to a different account.
When you attempt to validate the username, it will still return the username that initiated the first interactive login-shell, in my case “linuxtldr”.
$ su jake $ logname
After learning the difference, it’s debatable whether this command should be used when writing a shell script, as whoami is more efficient for this task.
Yet I have given you an example of this command in the shell script, and the answer to this question is up to you (leave it in the comment section).
Usage of the Logname Command in Shell Script
The following is a simple script that will check whether the username is “
backup” or not; if it is “
backup“, it will start the backup process or ask for the backup account.
if [[ "$(logname)" = 'backup' ]] then echo "Starting the backup process..." else echo "Require the backup user account." fi
What is $LOGNAME and $USER Shell Variable
$LOGNAME is the same as the
$USER environment variable set by your system to give you the name of the currently logged-in user.
There is a small difference between this variable and the logname command, followed by
$LOGNAMEvariable gives the name of the currently logged-in user in the terminal.
- Whereas, “
logname” prints the name of the interactively logged-in user who initiated the session.
The following is an example of the differences between both commands:
$ echo $LOGNAME $ logname $ sudo su $ echo $LOGNAME $ logname
As I’ve told you earlier, this variable is set by your system and can be easily modified, so it’s better to avoid using it as a medium to find the username of a logged-in user.
That’s all for this article for now.
If you have any questions or queries related to this topic, then feel free to ask them in the comment section.
Till then, peace!