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 user who is currently logged on to the system, 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:
whoami command can be used in shell scripts to check the name of the user running the script.
if [[ "$(whoami)" != "any_name" ]]; then echo "Only user 'any_name' can run this script." exit 1 fi
If the user name does not match with the given string, the script will echo a message and exit.
whoami command is also is handy for verifying the name of the user 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
id 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.