• Home
  • Linux
  • Ubuntu
  • Debian
  • CentOS
  • Linux Commands
  • About Us
  • Donate
TecNStuff
Menu
  • Home
  • Linux
  • Ubuntu
  • Debian
  • CentOS
  • Linux Commands
  • About Us
  • Donate

Whoami Command in Linux

Written by Admin, Updated On August 23, 2020
terminal, whoami
Whoami Command in Linux

The 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 whoami command:

whoami [OPTION]

To know the user who is currently logged on the system, run the command without any option:

whoami

It will show the output as following:

tecnstuff

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.

Alternative Commands#

To get the same output as whoami command, you can run the id command with the -un option.

You also get the username using the $USER environment variable which contains the name of the logged-in user:

echo $USER

Conclusion#

Using 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.

If our content helps you, please consider buying us a coffee

Thank you for your support.

Share On
Share on Facebook
Share on Twitter
Share on Reddit
Share on Tumblr
 Previous Article How to Install Postman on Ubuntu 20.04
Next Article   How to Install Apache Cassandra on CentOS 8

Related Posts

  • How to Install SSH Keys on Ubuntu 22.04

    How to Set up SSH Keys on Ubuntu 22.04

    January 7, 2023
  • How to Install Fail2ban on Ubuntu 22.04

    How to Install and Configure Fail2ban on Ubuntu 22.04

    December 5, 2022
  • How to Enable SSH on Ubuntu 22.04

    How to Enable SSH on Ubuntu 22.04

    December 1, 2022

Leave a Reply Cancel reply

DigitalOcean Referral Badge

Popular Posts

© 2020 TecNStuff All rights reserved. This website is using and storing cookies on your browser. By using this website you agree our Privacy Policy.  Follow us -  Twitter | Facebook