How to Find Current User Account in Linux
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
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}'
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:
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
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:
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
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:
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:
You can also delete user accounts using the userdel command:
You can also modify user accounts using usermod command:
I hope you will find this guide helpful.