Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get the current user's username in Bash?

Tags:

bash

I am writing a program in Bash that needs to get the user's username.

I have heard of a thing called whoami, but I have no idea what it does or how to use it.

What command do I use to get the current username?

like image 744
George Avatar asked Oct 10 '13 21:10

George


People also ask

How do I get the current username in Linux?

To quickly reveal the name of the logged in user from the GNOME desktop used on Ubuntu and many other Linux distributions, click the system menu in the top-right corner of your screen. The bottom entry in the drop-down menu is the user name.

How do I find a current user?

Type whoami and press Enter. Your current user name will be displayed.

What is username command in Linux?

There is no specific “username” command in Linux but there are other several sets of commands that let the user access the various users on the machine. 1. id: This command basically prints the information of real and effective user or in other words the current user.

Which command is used to get the user identity?

id command in Linux is used to find out user and group names and numeric ID's (UID or group ID) of the current user or any other user in the server.


2 Answers

On the command line, enter

whoami 

or

echo "$USER" 

To save these values to a variable, do

myvariable=$(whoami) 

or

myvariable=$USER 

Of course, you don't need to make a variable since that is what the $USER variable is for.

like image 85
4 revs, 4 users 80% Avatar answered Nov 16 '22 16:11

4 revs, 4 users 80%


An alternative to whoami is id -u -n.

id -u will return the user id (e.g. 0 for root).

like image 24
Brett Avatar answered Nov 16 '22 16:11

Brett