• 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 Use sed to Find and Replace String in Files

    How to Use sed to Find and Replace String in Files

    December 18, 2020
  • Linux Head Command

    Linux Head Command

    December 16, 2020
  • Rename Files and Directories in Linux

    How to Rename Files and Directories in Linux

    December 15, 2020

Leave a Reply Cancel reply

Popular Posts

  • How to Install Python 3.9 on Debian 10
    How to Install Python 3.9 on Debian 10 December 25, 2020
  • How to Install Notepad++ on Debian 10
    How to Install Notepad++ on Debian 10 December 23, 2020
  • How to Install Php 8 on Debian 10
    How to Install PHP 8 on Debian 10 January 2, 2021
  • How to Install Python 3.9 on CentOS 8
    How to Install Python 3.9 on CentOS 8 December 31, 2020
  • How to Install Php 8 on Ubuntu 20.04
    How to Install PHP 8 on Ubuntu 20.04 December 28, 2020
© 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