whoami – print current user name

whoami prints the username of the current user. Simple but essential for scripts and verifying identity.
Synopsis
whoami
Examples
Basic usage
$ whoami
greys
In scripts
#!/bin/bash
if [ "$(whoami)" != "root" ]; then
echo "This script must be run as root"
exit 1
fi
After sudo
$ whoami
greys
$ sudo whoami
root
With su
$ su - admin
$ whoami
admin
Alternatives
# Using id
$ id -un
greys
# Using environment variable
$ echo $USER
greys
# Using who
$ who am i
greys pts/0 2025-01-15 10:00
Tips
- $USER vs whoami:
$USERis faster but can be modified;whoamiqueries the system - In sudo:
whoamishows effective user,who am ishows login user - Use id for more info:
idshows UID, GID, and groups
See Also
Related Commands
- id — Print user and group IDs
- who — Show logged in users
- w — Show who is logged in and what they’re doing






