Skip to content
shellmap

Get the current username

Print the username running the current shell or script.

How to get the current username in each shell

Bashunix
whoami

`whoami` is short for `id -un` — prints the EFFECTIVE user (post-sudo). `$USER` is the LOGIN user (pre-sudo). Under `sudo`, `whoami` says `root` but `$SUDO_USER` retains your real login. `id -nu` is identical to `whoami`; `id -ng` is the effective primary group; `id` alone dumps uid/gid/groups.

Zshunix
whoami

Same external `whoami` + builtin `$USER`. zsh also auto-populates `$USERNAME` (== `$USER`) — the only shell that ships both. For tests, prefer `$USER` (more portable across bash/zsh/dash).

Fishunix
whoami

Same external. Fish variables: `$USER` (string), `$status` (last exit). Fish has no `$USERNAME` (that's zsh-only) — porting prompts that reference `$USERNAME` between shells needs care.

PowerShellwindows
$env:USERNAME

Short form: `$env:USERNAME` = `Alice`. Full domain form: `$env:USERDOMAIN\$env:USERNAME` = `CONTOSO\Alice`. AzureAD / Entra-joined: `whoami /upn` returns `[email protected]`. The Linux-flavoured `whoami` cmd also exists on Windows (`whoami.exe`, same as the Unix tool). For pwsh 7 cross-OS: `[System.Environment]::UserName` is bare username (works on macOS / Linux too where `USERNAME` env may be unset and `USER` is the variable).

cmd.exewindows
whoami

`whoami.exe` (in System32) — Windows Vista+. Plus env vars: `echo %USERNAME%` (short — no process spawn), `echo %USERDOMAIN%\%USERNAME%` (domain\user). For the full UPN: `whoami /upn` (Vista+, returns `[email protected]` for AD-joined). `whoami /groups` lists all group memberships.

Equivalents listed for Bash, Zsh, Fish, PowerShell, cmd.exe.

Gotchas & notes

  • **Effective user vs login user — `whoami` vs `$USER`**: under `sudo` (Linux/macOS) `whoami` returns `root` but `$USER` retains the original login (`alice`). `$SUDO_USER` is the bash variable holding the pre-sudo name. On Windows `runas /user:Administrator cmd.exe` opens a new shell where `whoami` and `%USERNAME%` BOTH change to Administrator (the env var is re-evaluated for the new process). The Linux pattern of "I sudoed but kept my identity in env vars" doesn't apply.
  • **`whoami /upn` vs `whoami /fqdn`**: AD-joined Windows hosts have two name forms — sAMAccountName (`alice`, ≤ 20 chars) and userPrincipalName (`[email protected]`, RFC822-shaped). `%USERNAME%` is sAMAccountName; `whoami /upn` is UPN; `whoami /fqdn` is the LDAP distinguished name (`CN=Alice,OU=Users,DC=contoso,DC=com`). Modern apps (M365, Azure AD, Okta) want UPN; legacy apps want sAMAccountName.
  • **SYSTEM / service contexts**: Windows services running as `LocalSystem` show `whoami = NT AUTHORITY\SYSTEM`. Tasks scheduled to run as SYSTEM see `%USERNAME%` = `<host>$` (machine account, dollar-suffix). On Linux, processes started by systemd as `User=` show that user; processes inherited from `init` (PID 1) on traditional sysv-init show `root`. Containers: `whoami` returns the container user, which may be `root` even when the container is run by an unprivileged host user (the Linux user namespace remaps).
  • **Cross-platform pwsh — `$env:USERNAME` is Windows-only**: pwsh 7 on Linux/macOS does NOT set `$env:USERNAME` (the env var is named `USER` on POSIX). Cross-platform-safe pwsh: `[System.Environment]::UserName` (.NET — works on every OS). Bash one-liner that works on every Unix-y shell + pwsh-on-POSIX: `${USER:-$(id -un)}` (fallback chain — env var first, then `id -un` if env is empty).

Related commands

Related tasks