Learn about the Whoami command on Linux

In this article we will cover the commands whoami.

As the name implies, orders whoami It will print the user ID name. In other words, it displays the name of the user who is currently logged in.

How to use the whoami

The syntax for the whoami command is as follows:

whoami [OPTION]

To display the name of the user who is currently logged into the system, type the command whoami Without any options:

whoami

The output will display the name of the currently logged in user or request commands. The whoami command can be used in shell scripts to verify the name of the username running the script.

Here is an example of using an if statement to compare the name of the username running the script against the specified string.

if [[ "$(whoami)" != "any_name" ]]; then
  echo "hanya user 'any_name' yang bisa menjalankan script ini."
  exit 1
fi

If the username does not match the specified string, the script will pop the echo and exit section.

Command whoami Also useful for checking username after switching to another user with command su.

whoami does not accept arguments. If you use arguments, whoami command will print an error output like:

whoami: extra operand ‘anything’
Try 'whoami --help' for more information.

Command whoami Only two options are accepted:

  • -hAnd the --help – Displays help messages and exits.
  • -VAnd the --version – Displays program version and output information.

Alternative orders

Run the commands id With options -un It will yield the same output by running a whoami:

id -un

Use commands id To get more information about a specific user.

Environment variables $USER Contains the name of the currently logged in user:

echo$USER

Conclusion

Command whoami Is a combination of the word “who am I? It prints the username associated with the current effective user ID. This is very important for a Linux system administrator to manage the operating system.

.

Source link

Leave a Reply

Your email address will not be published. Required fields are marked *