Linux / Unix Shell Script: Get Current User Name

I am working on a shell script. I need to find out the current user name. How do I find out the current user name under Bash or Ksh shell running on Linux or Unix like operating systems? How do I determine the current user account in Linux?

You can use the variables $USER, or $USERNAME which are not Bash builtins. These are, however, set as environmental variables in one of the Bash startup files. You can use the id command to get the same information.
Tutorial details
Difficulty level Easy
Root privileges No
Requirements Bash/ksh
Est. reading time 1m
a] $USER – Current user name.
b] $USERNAME – Current user name.
c] id command – Print current user name.
d] whoami command – Show current user name.


To get the current user name, type:

echo "$USER"

Get the current user name and store to a shell variable called $u:

echo "User name $u"

Sample outputs:

User name vivek

The syntax for id command is:

id -u -n

Sample outputs:


To print numeric UID, run:

id -u

Sample outputs:


The following script reads user name and store to a variable called _user _uid:

_user="$(id -u -n)"
_uid="$(id -u)"
echo "User name : $_user"
echo "User name ID (UID) : $_uid"

Shell script example

Make sure only root user can run the following script:

## get UID 
uid=$(id -u)
## Check for it
[ $uid -ne 0 ] && { echo "Only root may enable the nginx-chroot environment to the system."; exit 1; }
## Continue main logic with root user

A note about $EUID

This variable EUID is readonly. It expands to the effective user ID of the current user, initialized at shell startup. You can use $EUID to find out if user is root or not with the following syntax:

# Find out if you are root or not for admin tasks.
(( EUID )) && { echo 'Run this script with root priviliges.'; exit 1; } || echo 'Running as root, starting service...'

How to determine the current user account in Linux

We can use the who command as follows to print the current user account:

OR use the id command:
id -u -n

Summing up

We explained how you could find and get the current user’s username in a Bash script and shell prompt using various command-line options. See bash man page for more information by typing the following commands:
man bash

🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 4 comments so far... add one

CategoryList of Unix and Linux commands
Disk space analyzersdf duf ncdu pydf
File Managementcat cp mkdir tree
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Modern utilitiesbat exa
Network UtilitiesNetHogs dig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg glances gtop jobs killall kill pidof pstree pwdx time vtop
Searchingag grep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
4 comments… add one
  • Roman Jul 14, 2014 @ 8:15

    You forgotten regarding `whoami`

  • Sitthykun Oct 22, 2014 @ 3:11


  • ABC Mar 13, 2016 @ 11:45

    Thanks!! Great help!!

  • Titli Mar 8, 2021 @ 9:30

    Easy command to find the current username and account in Linux.

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre> for code samples. Still have questions? Post it on our forum