Whoami Command in Linux
2 min read
In this article, we will cover the
As its name suggests, the
whoami command prints the user name of the effective user ID. In other words, it displays the name of the currently logged-in user.
How to Use the
The syntax for the
whoami command is as follows:
To display the name of the currently logged user, invoke the command without any options:
Output similar to the following will be displayed on the screen, showing the name of the user invoking the command:
You can use the
whoami command in shell scripts to check the user’s name running the script.
if [[ "$(whoami)" != "any_name" ]]; then
echo "Only user 'any_name' can run this script."
If the user name does not match the given string, the script will echo a message and exit.
The whoami command is also handy for verifying the user’s name after switching to another user with the
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.
whoami command accepts only two options:
--help- Display a help message and exit.
--version- Shows the version information and exit.
id command with the
-un options produces the same output as running
command to obtain more information about a given user.
$USER environment variable
contains the name of the logged-in user:
whoami command is a compound of the words “Who am I?” and prints the name of the user associated with the current effective user ID.
If you have any questions or feedback, feel free to leave a comment.