How to use the command 'whoami' (with examples)
The whoami
command prints the username associated with the current effective user ID. It is useful for quickly identifying the currently logged-in user or verifying changes in the user ID after using commands like sudo
.
Use case 1: Display currently logged username
Code:
whoami
Motivation: This use case is helpful when you want to quickly determine the username of the currently logged-in user. It can be useful for troubleshooting or verifying that you are using the correct user account.
Explanation: The whoami
command, when used without any arguments, directly prints the username associated with the current effective user ID.
Example output:
johnsmith
In this example, the output shows the username “johnsmith”. This means that the user currently logged in or executing the command is “johnsmith”.
Use case 2: Display the username after a change in the user ID
Code:
sudo whoami
Motivation: This use case is useful when you want to verify the username after elevating privileges using the sudo
command. Since sudo
temporarily changes the user ID to the root or another specified user, checking the username with whoami
ensures that the change was successful.
Explanation: In this use case, the whoami
command is executed with sudo
. The sudo
command allows executing a command as another user, usually the root user. By using sudo
before whoami
, we ensure that the command is executed with root privileges, allowing us to view the username associated with the root user ID.
Example output:
root
In this example, the output shows the username “root”. This means that the command was executed with root privileges, and the user associated with the root user ID is “root”.