[proxy] web.archive.org← back | site home | direct (HTTPS) ↗ | proxy home | ◑ dark◐ light

Environment variables - ArchWiki

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

Note: Some environment variables are user-specific. Check by comparing the outputs of printenv as an unprivileged user and as root.

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 documentation: [1].

Each process stores their environment in the /proc/$PID/environ file. This file contained each key value pair delimited by a nul character (\x0). 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 /etc/locale.conf. 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 be only be present in some particular files.

The following files should be used for defining global environment variables on your system: /etc/environment, /etc/profile and shell specific configuration files. Each of these files has different limitations, so you should carefully select the appropriate one for your purposes.

In this example, we add ~/bin directory to the PATH for respective user. To do this, just put this in your preferred global environment variable config file (/etc/profile or /etc/bash.bashrc):

# If user ID is greater than or equal to 1000 & if ~/bin exists and is a directory & if ~/bin is not already in your $PATH
# then export ~/bin to your $PATH.
if [[ $UID -ge 1000 && -d $HOME/bin && -z $(echo $PATH | grep -o $HOME/bin) ]]
then
    export PATH="${PATH}:$HOME/bin"
fi

Per user

Note: The dbus daemon and the user instance of systemd do not inherit any of the environment variables set in places like ~/.bashrc etc. This means that, for example, dbus activated programs like Gnome Files will not use them by default. See Systemd/User#Environment variables.

You do not always want to define an environment variable globally. For instance, you might want to add /home/my_user/bin to the PATH variable but do not want all other users on your system to have that in their PATH too. Local environment variables can be defined in many different files:

To add a directory to the PATH for local usage, put following in ~/.bash_profile:

export PATH="${PATH}:/home/my_user/bin"

To update the variable, re-login or source the file: $ source ~/.bash_profile.

Graphical applications

To set environment variables for GUI applications, you can put your variables in xinitrc (or xprofile when using a display manager), for example:

~/.xinitrc
export PATH="${PATH}:~/scripts"
export GUIVAR=value

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 PATH variable in your current session, combined with the export command. As long as you do not log out, the PATH variable will be using the temporary settings. To add a session-specific directory to PATH, issue:

$ export PATH="${PATH}:/home/my_user/tmp/usr/bin"

Examples

The following section lists a number of common environment variables used by a Linux system and describes their values.

The DE environment variable needs to be exported before starting the window manager. For example:
~/.xinitrc
export DE="xfce"
exec openbox
This will make xdg-open use the more user-friendly exo-open, because it assumes it is running inside Xfce. Use exo-preferred-applications for configuring.

Note: It is advised not to include the current working directory (.) into your PATH for security reasons, as it may trick the user to execute vicious commands.

export EDITOR="$(if [[ -n $DISPLAY ]]; then echo 'gedit'; else echo 'nano'; fi)"
if [ -n "$DISPLAY" ]; then
    export BROWSER=firefox
else 
    export BROWSER=links
fi
ftp_proxy="ftp://192.168.0.1:21"
http_proxy="http://192.168.0.1:80"

Note: In /etc/profile, there is a comment that states "Man is much better than us at figuring this out", so this variable should generally be left as default, i.e. /usr/share/man:/usr/local/share/man

Using pam_env

The PAM module pam_env(8) loads the variables to be set in the environment from the following files: /etc/security/pam_env.conf, /etc/environment, ~/.pam_environment.

Here is an example of basic user directories configuration:

~/.pam_environment
# Setting variables that reuse your $HOME
XDG_CACHE_HOME   DEFAULT=@{HOME}/.cache
XDG_CONFIG_HOME  DEFAULT=@{HOME}/.config
XDG_DATA_HOME    DEFAULT=@{HOME}/.local/share

# You can reuse XDG_RUNTIME_DIR for runtime files
ICEAUTHORITY     DEFAULT=${XDG_RUNTIME_DIR}/ICEauthority

# You can reuse variables you already defined
GNUPGHOME        DEFAULT=${XDG_CONFIG_HOME}/gnupg

# You can define variables as VARIABLE=VALUE pair
EDITOR=nano

# Same as above
EDITOR           DEFAULT=nano

#Incorrect: you can't reuse other variables in VARIABLE=VALUE pair
#GNUPGHOME=${XDG_CONFIG_HOME}/gnupg

#Incorrect: missing {}
#GNUPGHOME        DEFAULT=$XDG_CONFIG_HOME/gnupg

Note: This file is read before everything, even ~/.{,bash_,z}profile and ~/.zshenv.

See also