The syntax for the whoami command is as follows:

whoami [OPTION]

 

To display the name of the user who is currently logged on to the system, invoke the command without any options:

whoami

 

Output similar to the following will be displayed on the screen, showing the name of the user invoking the command:

Output:
root

 

The whoami command can be used in shell scripts to check the name of the user running the script. Here is an example using an if statement to compare the name of the user running the script with a given string.

if [[ "$(whoami)" != "any_name" ]]; then
  echo "Only user 'any_name' can run this script."
  exit 1
fi

 

If the user name does not match with the given string, the script will echo a message and exit.

 

The whoami command is also is handy for verifying the name of the user after switching to another user with the su command.

 

whoami does not accept arguments. If you pass an argument, the command prints an error message:

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

 

The whoami command accepts only two options:

  • -h, --help - Display a help message and exit.
  • -V, --version - Shows the version information and exit.

 

Alternative Commands

 

Running the id command with the -un options produces the same output as running whoami:

whoami [OPTION]

 

Use the id command to obtain more information about a given user. The $USER environment variable contains the name of the logged-in user:

echo $USER

 

The whoami command is a compound of the words “Who am I?” and prints the name of the user associated with the current effective user ID.

 

Je li Vam ovaj odgovor pomogao? 0 Korisnici koji smatraju članak korisnim (0 Glasovi)