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?
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.
Type whoami and press Enter. Your current user name will be displayed.
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.
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.
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.
An alternative to whoami
is id -u -n
.
id -u
will return the user id (e.g. 0 for root).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With