whoami command will prints the user name of the currently logged-in user. In this article we will show you how to use the whoami command in linux.
Use of whoami Command
Following is the basic syntax for the
To know the user who is currently logged on the system, run the command without any option:
It will show the output as following:
In above output, you can see that the
tecnstuff user is currently logged in so it is showing that name. The
whoami command can be used in shell scripts to check the name of the user running the script.
For example, to check the user name which is running in script,
whoami will be used as following:
if [[ "$(whoami)" != "USER_NAME" ]]; then echo "Only user 'USER_NAME' is allowed." exit 1 fi
So if the output of the
whoami will not matched with the
USER_NAME then it will print the message and exit from the script.
Note : You should replace USER_NAME with your real user name in your Linux system.
It is very useful when you are switching to another user using su command and doing any stuff. At that time, you can verify the username using whoami command.
The whoami command accepts only two options:
- -h, –help – Display a help message and exit.
- -V, –version – Shows the version information and exit
If you will pass any other arguments with whoami command it will show error message like below:
whoami: extra operand ‘anything’ Try 'whoami --help' for more information.
To get the same output as
whoami command, you can run the
id command with the
You also get the username using the
$USER environment variable which contains the name of the logged-in user:
whoami command you can know the currently logged-in user name. It is very useful when you are switching to another users.
If you have any questions or suggestion, please leave a comment below.