Get username in Ubuntu
How To

How to Find the Username in Ubuntu

Sagar Sharma
Sagar Sharma

Table of Contents

The easiest way to find the username in Ubuntu is to use the whoami command and it will show the username of the currently logged-in user:

whoami

You can also use the $USER shell environment variable:

echo $USER

But that's so basic and may not hold the value as finding the logged-in user is pretty easy. It can be found by just looking at the terminal prompt as the default line starts by username@hostname.

The problem starts when you want to do the same but within the bash script.

So in this tutorial, I will walk you through two methods by which you can get the username from within the bash script:

  • Using the whoami command
  • Using the $USER environment variable

Let's start with the whoami command.

Using the whoami command

Before I walk you through the how-to part, let me share some basics so you can use the whoami or any other command in the bash script per your needs.

To use a command that prints something like uname -r to know the kernel version or using the date command to know the current date and time, you use a variable to store its data.

For example, here I used the username variable to store the value of the whoami command:

username=$(whoami)

Now, you can use this variable inside your bash script.

For your reference, here, I utilized the username variable with the echo command to print the username of the currently logged-in user:

#!/bin/bash

# Get the current username using whoami
username=$(whoami)

# Print the username
echo "The current username is: $username"

When you execute the above command, it will add additional text with the username:

Use the whoami command in the bash script

Using the $USER environment variable

🚧
The value of the environment variable can be changed using the envsubst command.

The $USER environment variable holds the name of the currently logged-in user but as I mentioned in the beginning, it can be changed so printing the value of the environment variable is a good idea:

echo $USER

If you get the username in return, go ahead with the shown guide.

Now the question is how is it any different than the whoami command?

Unlike the whoami command, you don't have to use a variable to store its value as the $USER itself is a variable and can be used directory as shown here:

#!/bin/bash

# Print the username using the $USER variable 
echo "The current username is: $USER"

When you execute the above script, you'll get the same result as the whoami command:

Use the $USER environment variable to find the username

There you have it!

More on listing users

While the above guide explained how you can utilize the username, what if you want to list down currently logged-in users?

Well, to answer that question, we wrote a detailed guide on how to find currently logged-in users in Ubuntu:

See Logged in Users on Ubuntu Linux
Wondering who else is currently logged in to your system? Here are various ways to see logged in users on Ubuntu.

And why stop at the currently logged in users? List all the available users on your Ubuntu system.

How to List Users in Ubuntu Command Line
Learn various ways of listing users in Ubuntu Linux. You’ll also learn the difference between the system and regular users.

I hope you will find this guide helpful.



Sagar Sharma

Sagar Sharma

A software engineer who loves to tinker with hardware till it gets crashed. While reviving my crashed system, you can find me reading literature, manga, or watering my plants.