whoami – print current user name

whoami command
whoami command

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: $USER is faster but can be modified; whoami queries the system
  • In sudo: whoami shows effective user, who am i shows login user
  • Use id for more info: id shows UID, GID, and groups

See Also

  • id — Print user and group IDs
  • who — Show logged in users
  • w — Show who is logged in and what they’re doing

Tutorials