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

George picture George · Oct 10, 2013 · Viewed 422.2k times · Source

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?

Answer

SethMMorton picture SethMMorton · Oct 10, 2013

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.