whoami Command in Linux: Display the Current User Name

By 

Updated on

3 min read

Linux Whoami Command

The whoami command prints the user name associated with the current effective user ID. In simple terms, it shows which user the current command is running as. This article explains how to use the whoami command, when it is useful, and how it compares with alternatives such as id -un and $USER.

How to Use the whoami Command

The syntax for the whoami command is as follows:

txt
whoami [OPTION]

To display the name of the current effective user, run the command without any options:

Terminal
whoami

Output similar to the following appears on the screen:

output
linuxize

The whoami command is useful when you need to confirm which account a command is running as. This matters after switching users with the su command or running a command with sudo .

For example, if you run whoami with sudo, the command shows root because the effective user changes:

Terminal
sudo whoami
output
root

You can also use whoami in shell scripts to check which user is running the script.

Here is an example using an if statement to compare the current user name with a given string:

sh
if [[ "$(whoami)" != "any_name" ]]; then
  echo "Only user 'any_name' can run this script."
  exit 1
fi

If the user name does not match the given string, the script prints a message and exits.

whoami does not accept arguments. If you pass an argument, the command prints an error message:

output
whoami: extra operand 'anything'
Try 'whoami --help' for more information.

The whoami command accepts only two options:

  • -h, --help - Display a help message and exit.
  • -V, --version - Show version information and exit.

Alternative Commands

Running the id command with the -un option produces the same output as running whoami:

Terminal
id -un

Use the id command when you need more information about the current user, such as the UID, GID, and group memberships.

The $USER environment variable also contains a user name:

Terminal
echo $USER

In many interactive shells, echo $USER prints the login name of the original session, while whoami prints the effective user for the current process. Because of that, the two can differ after sudo or other privilege changes.

Quick Reference

CommandDescription
whoamiPrint the current logged-in username
id -unSame output as whoami
echo $USERPrint username via environment variable
whoami --helpDisplay help and exit
whoami --versionShow version and exit

FAQ

What is the difference between whoami and id?
whoami prints only the effective user name. The id command prints full identity information, including the UID, GID, and group memberships. Running id -un produces the same output as whoami.

What does whoami return when running with sudo?
It usually returns root, because sudo runs the command with root as the effective user.

Can whoami be used in shell scripts?
Yes. It is commonly used to check whether a script is running as a specific user before performing privileged operations.

Why can whoami and echo $USER show different values?
whoami shows the effective user for the current process, while $USER often reflects the original login session. After a privilege change, the values may not match.

Conclusion

The whoami command is a simple way to display the current effective user name in Linux. When you need more identity details, use the id command to inspect the UID, GID, and group memberships.

Linuxize Weekly Newsletter

A quick weekly roundup of new tutorials, news, and tips.

About the authors

Dejan Panovski

Dejan Panovski

Dejan Panovski is the founder of Linuxize, an RHCSA-certified Linux system administrator and DevOps engineer based in Skopje, Macedonia. Author of 800+ Linux tutorials with 20+ years of experience turning complex Linux tasks into clear, reliable guides.

View author page