Beginners Guide for Whoami Command in Linux

Linux TLDR
Last Updated:
Reading time: 2 minutes

The whoami (concatenated of the strings “who,” “am,”, “i” as whoami) is a Linux command line utility that prints the username associated with the current effective user ID.

It comes in handy, especially while writing the shell script, like fetching the username within the script or executing the script for a specific user like root.

In this article, you will learn the usage of the whoami command, how to use it in shell scripts, and its alternative commands.

Tutorial Details

DescriptionChecking the Username of a Logged-In User
Difficulty LevelLow
Root or Sudo PrivilegesNo
Host System and ArchitectureUbuntu 22.10 (x64)
OS Compatibility Ubuntu, Manjaro, Fedora, etc.
Prerequisiteswhoami
Internet RequiredNo

Syntax of the Whoami Command

The whoami command takes only one argument as an option (optional).

$ whoami [OPTION]

The following is the list of all known options accepted by this command.

OptionDescription
--helpDisplay the help section.
--versionDisplay version information.

Usage of the Whoami Command in the Terminal

This command can be directly executed without specifying any flags to get the currently logged-in username.

$ whoami

Output:

Printing the username

If your system has multiple user accounts, you can use the whoami command to verify the username after switching to a different account.

$ su jake
$ whoami

Output:

Verifying the username of different accounts

Usage of the Whoami Command in Shell Script

The whoami command in shell script can be very useful if you want to check the user or execute the shell script for a specific user, like root.

For example, the following script will print “Execute this script as the root user.” Β when executed by a non-root user, or else it will print “You are running this script as the root user!” on the screen.

if [[ "$(whoami)" = 'root' ]]
then
	echo "You are running this script as the root user!"
else
	echo "Execute this script as the root user."
fi

Output:

Checking the username within the shell script

Whoami and its Substitute

There are additional commands that can print the username of the currently logged-in user in a manner similar to whoami, starting with

w command

The w command can be used to show the username of the currently logged-in user. It can also be used to show the usernames of everyone else who is logged in to your system.

$ w

Output:

Checking the username using the w command

who command

The who command is identical to the previous command that will print all the logged-in usernames with their terminal line and the time they logged in.

$ who

Output:

Checking the username using the who command

logname command

The logname command is quite identical to whoami, except with one difference: if the logname is executed with sudo, it will return the username.

However, if the whoami command is executed with sudo, it will return the effective username, which will be root.

Executing both the commands without appending sudo.

$ logname
$ whoami

Output:

Executing "logname" and "whoami" without sudo

Executing both the commands with sudo privileges.

$ sudo logname
$ sudo whoami

Output:

Executing "logname" and "whoami" with sudo

id command

The id command without any options provides user related information like the UID, GID, and list of groups the user is in.

$ id

Output:

Checking the username using the id command

Using the environment variable

By default, all Linux systems keep the username information in the $USER environment variable that can be printed using the echo command.

$ echo $USER

Output:

Checking the username using the environment variable

While using this method, keep in mind that the environment variable can be easily modified, which can lead to the wrong username.

That was the last example of checking the username of a logged-in user.

If you have any questions or queries related to this topic, then feel free to ask them in the comment section.

Till then, peace!

Join The Conversation

Users are always welcome to leave comments about the articles, whether they are questions, comments, constructive criticism, old information, or notices of typos. Please keep in mind that all comments are moderated according to our comment policy.

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.