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

Solution 1:

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.

Solution 2:

An alternative to whoami is id -u -n.

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