Environment variables in Linux can be defined as a set of variables which describe the the environment in which an application or program runs. These variables aren’t explicitly defined in a file or stored as such, but are defined when a program is executed.
List of Environment variables in Linux
In this module we will go over some environments which might be of interest to a Linux User and see what each of them signifies.
1. USER
This variable specifies the user you are logged in as. You can view this is with :
$ echo $USER
brucewayne
Note that this command still gives you your original username even when run with sudo privileges, because even though it is evoked with elevated privileges using sudo, the user remains the same.
$ sudo echo $USER
brucewayne
This can be utilitarian in scenarios when you want to know the user you are logged in as, a common scenario when it comes to CTFs.
2. HOME
This environment variable defines the home directory of our user. It is well known that when we use the following commands, they take us to the user’s home directory:
$ cd
Or,
$ cd ~
However, if we want to change the home directory to be some other directory, we can simply change the value of our HOME environment variable with :
$ export HOME=/path/to/directory
Learn more about setting environment variables here.
3. TERM
TERM is one of the environment variables in Linux set by the program that connects with the user logins. It defines the login terminal type. You can get a list of possible values of the TERM variable with :
$ ls /lib/terminfo/x/
x10term xnuppc+80x30 xnuppc-80x30-m xnuppc-256x96 xterm+edit xterm+sm+1002 xterm-basic xterm-vt220
x68k xnuppc+90x30 xnuppc-90x30 xnuppc-256x96-m xterm+indirect xterm+sm+1003 xterm-bold xterm-x10mouse
x68k-ite xnuppc+100x37 xnuppc-90x30-m xnuppc-b xterm+kbs xterm+sm+1005 xterm-color xterm-x11hilite
x820 xnuppc+112x37 xnuppc-100x37 xnuppc-f xterm+keypad xterm+sm+1006 xterm-direct xterm-x11mouse
x1700 xnuppc+128x40 xnuppc-100x37-m xnuppc-f2 xterm+noalt xterm+titlestack xterm-direct2 xterm-xf86-v32
x1700-lm xnuppc+128x48 xnuppc-112x37 xnuppc-m xterm+noapp xterm+tmux xterm-hp xterm-xf86-v33
x1720 xnuppc+144x48 xnuppc-112x37-m xnuppc-m-b xterm+osc104 xterm+vt+edit xterm-kitty xterm-xf86-v40
x1750 xnuppc+160x64 xnuppc-128x40 xnuppc-m-f xterm+pc+edit xterm+x10mouse xterm-mono xterm-xf86-v43
xdku xnuppc+200x64 xnuppc-128x40-m xnuppc-m-f2 xterm+pcc0 xterm+x11hilite xterm-new xterm-xf86-v44
xenix xnuppc+200x75 xnuppc-128x48 xtalk xterm+pcc1 xterm+x11mouse xterm-nic xterm-xf86-v333
xerox xnuppc+256x96 xnuppc-128x48-m xterm xterm+pcc2 xterm-8bit xterm-noapp xterm-xfree86
xerox-lm xnuppc+b xnuppc-144x48 xterm+88color xterm+pcc3 xterm-16color xterm-old xterm-xi
xerox820 xnuppc+basic xnuppc-144x48-m xterm+256color xterm+pce2 xterm-24 xterm-pcolor xterm.js
xerox1720 xnuppc+c xnuppc-160x64 xterm+256setaf xterm+pcf0 xterm-88color xterm-r5 xterm1
xfce xnuppc+f xnuppc-160x64-m xterm+alt+title xterm+pcf2 xterm-256color xterm-r6 xtermc
xiterm xnuppc+f2 xnuppc-200x64 xterm+alt1049 xterm+pcfkeys xterm-1002 xterm-sco xtermm
xl83 xnuppc-80x25 xnuppc-200x64-m xterm+app xterm+r6f2 xterm-1003 xterm-sun xterms
xnuppc xnuppc-80x25-m xnuppc-200x75 xterm+direct xterm+sl xterm-1005 xterm-utf8 xterms-sun
xnuppc+80x25 xnuppc-80x30 xnuppc-200x75-m xterm+direct2 xterm+sl-twm xterm-1006 xterm-vt52 xwsh
4. EDITOR
This defines the default editor which is used to edit text files on your system. Whichever text editor this variable points to shall be loaded when you use the edit command or use shortcuts like Ctrl+X+E
This same editor is used when invoking commands like sudoedit. It is generally preferred to set this variable to point at vim instead of nano [Read the difference between vim and nano]
5. PATH
PATH is probably the most interesting Environment Variables in Linux according to me. It basically consists of a list of directories were the system looks for a command’s executable/script. For example, when we type a command like ls, the system searches for an executable or a script with the same name in the directories listed in PATH from the start and executes the first instance of ls it finds.
Sometimes, you might want to have your own set of special binaries which you might want to run as commands. You can do this by adding the directory in which they are contained to the PATH.
$ echo $PATH
/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games
$ export PATH=$PATH:/path/to/directory
$ echo $PATH
/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games:/path/to/directory
However, you must be careful as if any of the binaries don’t have the same name as any other system binaries/scripts because the system executes the first instance of the binary/script it finds.
Conclusion
Apart from the Environment Variables in Linux listed here, there are several other which might be of interest to users depending on the context. You can also modify these Environment variables in Linux to alter the Environment the of programs. To know how we can alter the Environment Variables, check out this post.