Skip to main content
Quick Tip

How to Find Current User Account in Linux

Here's a quick tip about getting the current user detail in Linux command line.

Sagar Sharma

Warp Terminal

The easiest way to find the current user is to open a terminal that will show the username and hostname.

Confused? Let me show you:

Another way is to use the $USER environment variable.

But what if you want to use this data in the bash script? Or do you want to find the current user in the bash script? Well, this guide will address such questions in more detail.

Find the current user account in Linux

In this tutorial, I will walk you through the following ways to find the current user account in Linux:

  • Using the who command
  • Using the whoami command (no additional flags required)
  • Using the $USER environment variable (best for bash script)

Each of these command explanations will include command line execution as well as how it can be used in the bash script so you can get the most out of each one.

1. Using the who command

To know the currently logged-in user through the who command, all you have to do is execute the who command in the following manner:

who
Use the who command in terminal prompt to know the currently logged in user in Linux

You'll see a current user along with some additional information where:

  • :1 indicates that the user is connected to the second graphical display (X Windows session).
  • 2023-12-13 16:46 it is the date and time when the user logged in to the system.

If you were to use the who command to get the username only, you'd have to make some changes.

Here, I used the awk command to print only the username removing all the other additional information:

who | awk '{print $1}'
Use the who command with the awk command to print only the username of the current user in linux

Using the who command in the bash script

To use the who command to find the current user account, you can use the who command with the awk argument and assign it to a variable as shown here:

#!/bin/bash

# Get the current user account using who and awk
current_user=$(who | awk '{print $1}')

echo "The current user is: $current_user"

When executed, it will give you the following output:

Use the who command in bash script to find the current user account in Linux

2. Using the whoami command

Unlike the who command, when you execute the whoami command in Linux, it will only print the username of the currently logged-in user:

whoami
Use whoami command to find the current user account in linux

Yep, no additional flags are required! Straight to the point.

Using the whoami command in the bash script

If you want to find the current user account using the whoami command, it can be evaluated in the bash script in the following manner:

#!/bin/bash

# Get the currently logged-in user using whoami
current_user=$(whoami)

echo "The currently logged-in user is: $current_user"

If you decide to use the above script, expect the following output:

Use the whoami command in bash script to find the current user account in linux

3. Using the $USER environment variable

To use the $USER environment variable in the bash script to find the current user, you'll have to pair it with the echo command as shown:

echo $USER
Use the echo with the $USER variable to find the current user in Linux

Now, let's have a look at the bash script part where it is meant to be used!

Using the $USER environment variable in the bash script

If you wish to use the bash script to find the current user, this might be the perfect example as you don't have to assign it to any variable.

Sure, you can achieve similar results with the commands (using $(command)) but still, I consider this the easiest way.

Here's how you can use the $USER environment variable in the bash script:

#!/bin/bash

echo "The current user is: $USER"

Yes, a simple one-liner, and when executed, it will get you the same results as the two previous commands did:

Use the $USER environment variable in bash script to find the current user account in Linux

Here's how to manage multiple users

If you are a sysadmin and want users to force users to change their password at the next login, here's a detailed guide explaining how to force users to change password:

Force Linux User to Change Password at Next Login
Think the passwords need to be changed by a certain user? Here’s how to force a Linux user to change the password at the next login.

You can also delete user accounts using the userdel command:

How to Delete Users in Ubuntu Linux With Userdel Command
If you want to delete an existing user in Ubuntu or any other Linux distribution, you can use the userdel command in the terminal.

You can also modify user accounts using usermod command:

8 Essential Examples of Usermod Command in Linux
The usermod command in Linux allows you to modify a user account in various ways. Check out what settings you can modify in this tutorial.

I hope you will find this guide helpful.

Sagar Sharma