whoami Command in Linux: Display the Current User Name

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:
whoami [OPTION]To display the name of the current effective user, run the command without any options:
whoamiOutput similar to the following appears on the screen:
linuxizeThe 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:
sudo whoamirootYou 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:
if [[ "$(whoami)" != "any_name" ]]; then
echo "Only user 'any_name' can run this script."
exit 1
fiIf 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:
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:
id -unUse 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:
echo $USERIn 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
| Command | Description |
|---|---|
whoami | Print the current logged-in username |
id -un | Same output as whoami |
echo $USER | Print username via environment variable |
whoami --help | Display help and exit |
whoami --version | Show 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.
Tags
Linuxize Weekly Newsletter
A quick weekly roundup of new tutorials, news, and tips.
About the authors

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