Skip to content

whoami Command: Know Your User Identity

Summary

The whoami command is a simple utility that displays the effective username of the current user. It's a quick way to confirm which user you are logged in as, especially in environments where you might switch between different user accounts.

Introduction

The whoami command is a fundamental Linux utility that provides a straightforward function: it prints the username associated with the current effective user ID. This command is particularly useful in scripts or when working on shared systems where it's crucial to verify your current user identity. It's part of the GNU coreutils package and is available on virtually all Linux distributions.

Use case and Examples

Basic Usage: Displaying the Username

whoami
This command simply prints the username of the currently logged-in user. For example, if you're logged in as "john", the output would be "john".

Scripting: Using whoami in a script

#!/bin/bash
USER=$(whoami)
echo "The current user is: $USER"
This script uses the whoami command to retrieve the username and store it in a variable called USER. Then, it prints a message including the username.

Verifying User After sudo

sudo whoami
After using sudo, running whoami will typically display "root", confirming you're executing commands with root privileges.

Commonly used flags

Flag Description Example
--help Display help information and exit. whoami --help
--version Output version information and exit. whoami --version
(None) Without any flags, whoami simply prints the effective username. whoami


Share on Share on

Comments