Environment Variable for Username

The Linux version environment variable to refer to the current user is USER.

To get the output you'd like to see use $USER in your command:

echo $USER

And to use it in another string such as a path:

/path/with/$USER/in/it

Will expand the username within the string. Though you should protect variable expansions with curly braces when a variable is directly next to other characters. It's not so important in this example as the USER variable is bounded by non-alphanumeric characters, but would be important in the following case:

/path/to/${USER}directory

(Thanks to ByteCommander for the tip in comments)

Another useful one to know is $HOME for the user's home directory.


$USER

will change it into your username.

Example

rinzwind@schijfwereld:~$ echo $USER
rinzwind

There are more...

~$ cd /tmp
:/tmp$ echo $HOME
/home/rinzwind
:/tmp$ echo $PWD
/tmp
$ echo $HOSTNAME
schijfwereld

To list the current values of all environment variables:

env

Keep in mind that variable names are case-sensitive


To expand on this. You can create your own too:

export var=value

will create $var with value.

$ export var=1111
rinzwind@schijfwereld:~$ echo $var
1111

There might be a caveat if you plan to read the $USER environment variable in a command starting with sudo.

Bash variable expansion takes place before executing sudo to switch users, that means the $USER variable gets read from the current environment before sudo switches to root.

$ echo $USER
bytecommander

$ sudo echo $USER
bytecommander

If this is not intended and you require a method that will return the name of the user as whom it really runs (normally "root"), you have at least three options to achieve that:

  • Run a bash interpreter as root and pass it the command which contains $USER. You must make sure that the command is enclosed with single quotes to prevent the current Bash interpreter from doing the variable expansion:

    sudo bash -c 'echo $USER'
    
  • Use a command output instead of the $USER environment variable.

    There are mainly two commands which would be useful here, whoami and id -un:

    $ whoami
    bytecommander
    
    $ sudo whoami
    root
    
    $ id -un
    bytecommander
    
    $ sudo id -un
    root
    

    More information about those commands can be found by typing man whoami and man id.

You can use these commands like a variable and embed them into a string (e.g. a directory path) like this, using Bash's command substitution syntax. Here are two examples which cd into a directory named after the current user:

cd /path/to/$(whoami)folder
cd /path/to/$(id -un)folder

As the other answers say, $USER is usually set to the name of the current user account.

But at least on my system (Ubuntu 14.04) the $USER environment variable is not set for cron jobs. Instead, you can use $LOGNAME (POSIX), which is part of the environment for cron jobs.

According to the environ(7) man page (type man 7 environ or man environ.7 to read it), $USER is used by BSD-derived programs and $LOGNAME is used by System-V-derived programs. They should have the same value if they're both set. The existence of both is an historical accident. (There could be cases where $USER is set and $LOGNAME isn't, but I don't know of any.)

The environ man page also documents a number of other common environment variables. (It doesn't document all environment variables because it would be impossible to do so.)