The whoami command in Linux prints the effective username associated with the active shell session. It is widely used in shell scripts to verify whether a user has root privileges (EUID 0) before executing privileged administrative tasks.

Terminal Execution & sudo Context Comparison

Terminal Sessionbash
# 1. Standard user execution
$ whoami
ubuntu
 
# 2. Privileged root subshell execution via sudo
$ sudo whoami
root

Using whoami in Bash Administrative Scripts

deploy.shbash
#!/usr/bin/env bash
 
# Enforce root execution requirement before running deployment script
if [ "$(whoami)" != "root" ]; then
    echo "ERROR: This script must be run as root (use sudo ./deploy.sh)." >&2
    exit 1
fi
 
echo "Root privileges confirmed. Executing system deployment..."