Environment variables
An environment variable is a named object that contains data used by one or more applications. In simple terms, it is a variable with a name and a value. The value of an environmental variable can for example be the location of all executable files in the file system, the default editor that should be used, or the system locale settings. Users new to Linux may often find this way of managing settings a bit unmanageable. However, environment variables provide a simple way to share configuration settings between multiple applications and processes in Linux.
Utilities
The coreutils package contains the programs printenv and env. To list the current environmental variables with values:
$ printenv
The env utility can be used to run a command under a modified environment. The following example will launch xterm with the environment variable EDITOR set to vim. This will not affect the global environment variable EDITOR.
$ env EDITOR=vim xterm
The Bash builtin set allows you to change the values of shell options and set the positional parameters, or to display the names and values of shell variables. For more information, see the set builtin documentation.
Each process stores their environment in the /proc/$PID/environ file. This file contains each key value pair delimited by a nul character (). A more human readable format can be obtained with sed, e.g. sed 's:\x0:\n:g' /proc/$PID/environ.
Defining variables
Globally
Most Linux distributions tell you to change or add environment variable definitions in /etc/profile or other locations. Keep in mind that there are also package-specific configuration files containing variable settings such as . Be sure to maintain and manage the environment variables and pay attention to the numerous files that can contain environment variables. In principle, any shell script can be used for initializing environmental variables, but following traditional UNIX conventions, these statements should only be present in some particular files.
The following files can be used for defining global environment variables on your system, each with different limitations:
- is used by the pam_env module and is shell agnostic so scripting or glob expansion cannot be used. The file only accepts pairs.
/etc/profileinitializes variables for login shells only. It does, however, run scripts and can be used by all Bourne shell compatible shells.- Shell specific configuration files - Global configuration files of your shell, initializes variables and runs scripts. For example Bash#Configuration files or Zsh#Startup/Shutdown files.
In this example, we add ~/bin directory to the for respective user. To do this, just put this in your preferred global environment variable configuration file (/etc/profile or ):
Per user
You do not always want to define an environment variable globally. For instance, you might want to add /home/my_user/bin to the variable but do not want all other users on your system to have that in their too. Local environment variables can be defined in many different files:
- User configuration files of your shell, for example Bash#Configuration files or Zsh#Startup/Shutdown files.
- systemd user environment variables are read from .
To add a directory to the for local usage, put following in :
export PATH="${PATH}:/home/my_user/bin"
To update the variable, re-login or source the file: .
Graphical environment
If an environment variable only affects graphical applications, you may want to restrict the scope of it by only setting it within the graphical session.
To set environment variables only for a specific application instead of the whole session, edit the application's .desktop file. See Desktop entries#Modify environment variables for instructions.
Using a startup script
Some graphical environments, (e.g. KDE Plasma) support executing shell scripts at login: they can be used to set environment variables. See KDE#Autostart for example.
Xorg environment
The procedure for modifying the environment of the Xorg session depends on how it is started:
- Most display managers source xprofile.
- startx and SLiM execute xinitrc.
- XDM executes ; see XDM#Defining the session.
Though the end of the script depends on which file it is, and any advanced syntax depends on the shell used, the basic usage is universal:
~/.xprofile, ~/.xinitrc, or ~/.xsession
... export ''GUI_VAR''=''value'' ...
Wayland environment
Since Wayland does not initiate any Xorg related files, GDM and KDE Plasma source systemd user environment variables instead.
No other display managers supporting Wayland sessions (e.g. SDDM) provide support for this yet.
Per session
Sometimes even stricter definitions are required. One might want to temporarily run executables from a specific directory created without having to type the absolute path to each one, or editing shell configuration files for the short time needed to run them.
In this case, you can define the variable in your current session, combined with the export command. As long as you do not log out, the variable will be using the temporary settings. To add a session-specific directory to , issue:
$ export PATH="${PATH}:/home/my_user/tmp/usr/bin"
Using pam_env
The PAM module loads the variables to be set in the environment from the following files in order: , (and the deprecated ).
must consist of simple VARIABLE=value pairs on separate lines, for example:
EDITOR=nano
and share the following format:
VARIABLE [DEFAULT=value] [OVERRIDE=value]
and are special variables that expand to what is defined in . The following example illustrates how to expand the environment variable into another variable:
XDG_CONFIG_HOME DEFAULT=@{HOME}/.config
The format also allows to expand already defined variables in the values of other variables using ${VARIABLE} , like this:
GOPATH DEFAULT=${XDG_DATA_HOME}/go
VARIABLE=value pairs are also allowed, but variable expansion is not supported in those pairs. See for more information.
Examples
The following section lists a number of common environment variables used by a Linux system and describes their values.
- is a freedesktop.org variable containing a colon separated list of strings that the current desktop environment identifies as . Standardized values for actively developed environments are , , , ,
LXQt, , , , ,EDE, , and .- Cinnamon was registered later than the rest of the desktop environments. As a result, some software may still be expecting its pre-registration value , such as Qt versions as recent as 6.3 .
- is similar to , but only permits a single string. Despite its name, it is not standardized by freedesktop.org.
DEis a legacy variable indicating the desktop environment being used. There is no central documentation for what possible values are, but xdg-utils provides a reference for many desktop environments.
- is another legacy variable, similar to
DEbut less common.
- is a variable sometimes used to choose the window manager to be used in a desktop environment, as opposed to the other variables here which are set by the already chosen display manager or desktop environment, for other programs to read.
- contains a colon-separated list of directories in which your system looks for executable files. When a regular command (e.g. ls, systemctl or pacman) is interpreted by the shell (e.g. bash or zsh), the shell looks for an executable file with the same name as your command in the listed directories, and executes it. To run executables that are not listed in , a relative or absolute path to the executable must be given, e.g. or .
- contains the path to the home directory of the current user. This variable can be used by applications to associate configuration files and such like with the user running it.
- contains the path to the working directory.
OLDPWDcontains the path to the previous working directory, that is, the value of before last cd was executed.
- contains the type of the running terminal, e.g. . It is used by programs running in the terminal that wish to use terminal-specific capabilities.
- contains the location of incoming email. The traditional setting is
/var/spool/mail/$LOGNAME.
- and contains FTP and HTTP proxy server, respectively:
ftp_proxy="ftp://192.168.0.1:21" http_proxy="http://192.168.0.1:80"
- contains a colon-separated list of directories in which man searches for the man pages.
- contains a colon-separated list of directories in which the info command searches for the info pages, e.g.,
- can be used to to set a time zone different to the system zone for a user. The zones listed in can be used as reference, for example
TZ=":/usr/share/zoneinfo/Pacific/Fiji". When pointing the variable to a zoneinfo file, it should start with a colon per the GNU manual.
Default programs
- contains the path to the user's preferred shell. Note that this is not necessarily the shell that is currently running, although Bash sets this variable on startup.
- contains command to run the program used to list the contents of files, e.g., .
EDITORcontains the command to run the lightweight program used for editing files, e.g.,/usr/bin/nano. For example, you can write an interactive switch between gedit under X or nano, in this example:
export EDITOR="$(if [[ -n $DISPLAY ]]; then echo 'gedit'; else echo 'nano'; fi)"
- contains command to run the full-fledged editor that is used for more demanding tasks, such as editing mail (e.g., , vim, emacs etc).
- contains the path to the web browser. Helpful to set in an interactive shell configuration file so that it may be dynamically altered depending on the availability of a graphic environment, such as X:
if [ -n "$DISPLAY" ]; then
export BROWSER=firefox
else
export BROWSER=links
fi