Difference between revisions of "Environment variables"

From ArchWiki
Jump to navigation Jump to search
m (xdg-open now redirects to Default applications, already linked)
m (→‎Per user: link to sourcing a file)
 
(59 intermediate revisions by 27 users not shown)
Line 1: Line 1:
 
[[Category:System administration]]
 
[[Category:System administration]]
 
[[de:Umgebungsvariablen]]
 
[[de:Umgebungsvariablen]]
 +
[[es:Environment variables]]
 
[[ja:環境変数]]
 
[[ja:環境変数]]
 +
[[pt:Environment variables]]
 
[[ru:Environment variables]]
 
[[ru:Environment variables]]
[[zh-cn:Environment variables]]
+
[[zh-hans:Environment variables]]
 
{{Related articles start}}
 
{{Related articles start}}
 
{{Related|Default applications}}
 
{{Related|Default applications}}
Line 18: Line 20:
 
{{Note|Some environment variables are user-specific. Check by comparing the outputs of ''printenv'' as an unprivileged user and as ''root''.}}
 
{{Note|Some environment variables are user-specific. Check by comparing the outputs of ''printenv'' as an unprivileged user and as ''root''.}}
  
The {{ic|env}} utility can be used to run a command under a modified environment. The following example will launch ''xterm'' with the environment variable {{ic|EDITOR}} set to {{ic|vim}}. This will not affect the global environment variable {{ic|EDITOR}}.
+
The ''env'' utility can be used to run a command under a modified environment. The following example will launch ''xterm'' with the environment variable {{ic|EDITOR}} set to {{ic|vim}}. This will not affect the global environment variable {{ic|EDITOR}}.
  
 
  $ env EDITOR=vim xterm
 
  $ 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: [http://www.gnu.org/software/bash/manual/bash.html#The-Set-Builtin].
+
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 [http://www.gnu.org/software/bash/manual/bash.html#The-Set-Builtin the set builtin documentation].
  
Each process stores their environment in the {{ic|/proc/$PID/environ}} file. This file contained each key value pair delimited by a nul character ({{ic|\x0}}). A more human readable format can be obtained with [[sed]], e.g. {{ic|sed 's:\x0:\n:g' /proc/$PID/environ}}.
+
Each process stores their environment in the {{ic|/proc/$PID/environ}} file. This file contains each key value pair delimited by a nul character ({{ic|\x0}}). A more human readable format can be obtained with [[sed]], e.g. {{ic|sed 's:\x0:\n:g' /proc/$PID/environ}}.
  
 
== Defining variables ==
 
== Defining variables ==
Line 30: Line 32:
 
=== Globally ===
 
=== Globally ===
  
Most Linux distributions tell you to change or add environment variable definitions in {{ic|/etc/profile}} or other locations. 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.  
+
Most Linux distributions tell you to change or add environment variable definitions in {{ic|/etc/profile}} or other locations. Keep in mind that there are also package-specific configuration files containing variable settings such as {{ic|/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 only be present in some particular files.  
  
The following files should be used for defining global environment variables on your system: {{ic|/etc/profile}}, {{ic|/etc/bash.bashrc}} and {{ic|/etc/environment}}. Each of these files has different limitations, so you should carefully select the appropriate one for your purposes.
+
The following files should be used for defining global environment variables on your system: {{ic|/etc/environment}}, {{ic|/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.
  
*{{ic|/etc/profile}} initializes variables for login shells ''only''. It does, however, run scripts and can be used by all [[wikipedia:Bourne shell|Bourne shell]] compatible shells.
+
* {{ic|/etc/environment}} is used by the pam_env module and is shell agnostic so scripting or glob expansion cannot be used. The file only accepts {{ic|1=''variable=value''}} pairs. See {{man|8|pam_env}} and {{man|5|pam_env.conf}} for details.
*{{ic|/etc/bash.bashrc}} initializes variables for interactive shells ''only''. It also runs scripts but (as its name implies) is Bash specific.
+
* Global configuration files of your [[shell]], initializes variables and runs scripts. For example [[Bash#Configuration files]] or [[Zsh#Startup/Shutdown files]].
*{{ic|/etc/environment}} is used by the PAM-env module and is agnostic to login/non-login, interactive/non-interactive and also Bash/non-Bash, so scripting or glob expansion cannot be used. The file only accepts {{ic|1=''variable=value''}} pairs.
+
* {{ic|/etc/profile}} initializes variables for login shells ''only''. It does, however, run scripts and can be used by all [[wikipedia:Bourne shell|Bourne shell]] compatible shells.
  
 
In this example, we add {{ic|~/bin}} directory to the {{ic|PATH}} for respective user. To do this, just put this in your preferred global environment variable config file ({{ic|/etc/profile}} or {{ic|/etc/bash.bashrc}}):
 
In this example, we add {{ic|~/bin}} directory to the {{ic|PATH}} for respective user. To do this, just put this in your preferred global environment variable config file ({{ic|/etc/profile}} or {{ic|/etc/bash.bashrc}}):
Line 45: Line 47:
 
if [[ $UID -ge 1000 && -d $HOME/bin && -z $(echo $PATH | grep -o $HOME/bin) ]]
 
if [[ $UID -ge 1000 && -d $HOME/bin && -z $(echo $PATH | grep -o $HOME/bin) ]]
 
then
 
then
     export PATH=$HOME/bin:${PATH}
+
     export PATH="${PATH}:$HOME/bin"
 
fi
 
fi
 
</nowiki>}}
 
</nowiki>}}
Line 51: Line 53:
 
=== Per user ===
 
=== 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 Nautilus will not use them by default. See [[Systemd/User#Environment variables]].}}
+
{{Note|The dbus daemon and the user instance of systemd do not inherit any of the environment variables set in places like {{ic|~/.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 {{ic|/home/my_user/bin}} to the {{ic|PATH}} variable but do not want all other users on your system to have that in their {{ic|PATH}} too. Local environment variables can be defined in many different files:
 
You do not always want to define an environment variable globally. For instance, you might want to add {{ic|/home/my_user/bin}} to the {{ic|PATH}} variable but do not want all other users on your system to have that in their {{ic|PATH}} too. Local environment variables can be defined in many different files:
  
# Configuration files of your shell, for example [[Bash#Configuration files]] or [[Zsh#Startup/Shutdown files]].
+
* {{ic|~/.pam_environment}} is the user specific equivalent of {{ic|/etc/security/pam_env.conf}} [https://github.com/linux-pam/linux-pam/issues/6], used by pam_env module. See {{man|8|pam_env}} and {{man|5|pam_env.conf}} for details.
# {{ic|~/.profile}} is used by many shells as fallback, see [[wikipedia:Unix shell#Configuration files]].
+
* User configuration files of your [[shell]], for example [[Bash#Configuration files]] or [[Zsh#Startup/Shutdown files]].
# {{ic|~/.pam_environment}} is the user specific equivalent of {{ic|/etc/environment}}, used by PAM-env module. See {{ic|pam_env(8)}} and {{ic|pam_env.conf(5)}} for details.
+
* [[systemd]] will load environment variables from {{ic|~/.config/environment.d/*.conf}} see {{man|5|environment.d}} and https://wiki.gnome.org/Initiatives/Wayland/SessionStart.
  
 
To add a directory to the {{ic|PATH}} for local usage, put following in {{ic|~/.bash_profile}}:
 
To add a directory to the {{ic|PATH}} for local usage, put following in {{ic|~/.bash_profile}}:
Line 63: Line 65:
 
  export PATH="${PATH}:/home/my_user/bin"
 
  export PATH="${PATH}:/home/my_user/bin"
  
To update the variable, re-login or ''source'' the file: {{ic|$ source ~/.bash_profile}}.
+
To update the variable, re-login or [[source]] the file: {{ic|$ source ~/.bash_profile}}.
  
==== Graphical applications ====
+
==== Graphical environment ====
  
To set environment variables for GUI applications, you can put your variables in [[xinitrc]] (or [[xprofile]] when using a [[display manager]]), for example:
+
Environment variables for [[Xorg]] applications can be set in [[xinitrc]], or in [[xprofile]] when using a [[display manager]], for example:
  
{{hc|1=~/.xinitrc|2=
+
{{hc|~/.xinitrc|2=
 
export PATH="${PATH}:~/scripts"
 
export PATH="${PATH}:~/scripts"
export GUIVAR=value}}
+
export GUIVAR=value
 +
}}
 +
 
 +
Applications running on [[Wayland]] may use [[Systemd/User#Environment variables|systemd user environment variables]] instead, as Wayland does not initiate any Xorg related files:
 +
 
 +
{{hc|~/.config/environment.d/envvars.conf|2=
 +
PATH=$PATH:~/scripts
 +
GUIVAR=value
 +
}}
 +
 
 +
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.
 +
 
 +
{{Tip|[[KDE Plasma]] supports executing shell scripts at login and even before launching Plasma, they can be used to set environment variables. See [[KDE#Autostart]].}}
  
 
=== Per session ===
 
=== 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 {{ic|~/.bash_profile}} for the short time needed to run them.
+
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 {{ic|PATH}} variable in your current session, combined with the ''export'' command. As long as you do not log out, the {{ic|PATH}} variable will be using the temporary settings. To add a session-specific directory to {{ic|PATH}}, issue:
 
In this case, you can define the {{ic|PATH}} variable in your current session, combined with the ''export'' command. As long as you do not log out, the {{ic|PATH}} variable will be using the temporary settings. To add a session-specific directory to {{ic|PATH}}, issue:
Line 85: Line 99:
 
The following section lists a number of common environment variables used by a Linux system and describes their values.
 
The following section lists a number of common environment variables used by a Linux system and describes their values.
  
*{{ic|DE}} indicates the ''D''esktop ''E''nvironment being used. [[xdg-open]] will use it to choose more user-friendly file-opener application that desktop environment provides. Some packages need to be installed to use this feature. For [[GNOME]], that would be {{pkg|libgnome}}; for [[Xfce]] this is {{pkg|exo}}. Recognised values of {{ic|DE}} variable are: {{ic|gnome}}, {{ic|kde}}, {{ic|xfce}}, {{ic|lxde}} and {{ic|mate}}.
+
* {{ic|DE}} indicates the ''D''esktop ''E''nvironment being used. [[xdg-open]] will use it to choose more user-friendly file-opener application that desktop environment provides. Some packages need to be installed to use this feature. For [[GNOME]], that would be {{AUR|libgnome}}; for [[Xfce]] this is {{pkg|exo}}. Recognised values of {{ic|DE}} variable are: {{ic|gnome}}, {{ic|kde}}, {{ic|xfce}}, {{ic|lxde}} and {{ic|mate}}.
  
 
:The {{ic|DE}} environment variable needs to be exported before starting the window manager. For example:
 
:The {{ic|DE}} environment variable needs to be exported before starting the window manager. For example:
Line 96: Line 110:
 
: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.
 
: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.
  
*{{ic|DESKTOP_SESSION}} is similar to {{ic|DE}}, but used in [[LXDE]] desktop enviroment: when {{ic|DESKTOP_SESSION}} is set to {{ic|LXDE}}, ''xdg-open'' will use ''pcmanfm'' file associations.
+
* {{ic|DESKTOP_SESSION}} is similar to {{ic|DE}}, but used in [[LXDE]] desktop environment: when {{ic|DESKTOP_SESSION}} is set to {{ic|LXDE}}, ''xdg-open'' will use ''pcmanfm'' file associations.
  
*{{ic|PATH}} contains a colon-separated list of directories in which your system looks for executable files. When a regular command (e.g., ''ls'', ''rc-update'' or ''ic|emerge'') 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 {{ic|PATH}}, the absoute path to the executable must be given: {{ic|/bin/ls}}.
+
* {{ic|PATH}} 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 {{ic|PATH}}, a relative or absolute path to the executable must be given, e.g. {{ic|./a.out}} or {{ic|/bin/ls}}.
  
 
{{Note|It is advised not to include the current working directory ({{ic|.}}) into your {{ic|PATH}} for security reasons, as it may trick the user to execute vicious commands.}}
 
{{Note|It is advised not to include the current working directory ({{ic|.}}) into your {{ic|PATH}} for security reasons, as it may trick the user to execute vicious commands.}}
  
*{{ic|HOME}} 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.
+
* {{ic|HOME}} 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.
 +
 
 +
* {{ic|PWD}} contains the path to your working directory.
 +
 
 +
* {{ic|OLDPWD}} contains the path to your previous working directory, that is, the value of {{ic|PWD}} before last ''cd'' was executed.
 +
 
 +
* {{ic|TERM}} contains the type of the running terminal, e.g. {{ic|xterm-256color}}. It is used by programs running in the terminal that wish to use terminal-specific capabilities.
 +
 
 +
* {{ic|MAIL}} contains the location of incoming email. The traditional setting is {{ic|/var/spool/mail/$LOGNAME}}.
 +
 
 +
* {{ic|ftp_proxy}} and {{ic|http_proxy}} contains FTP and HTTP proxy server, respectively:
 +
ftp_proxy="<nowiki>ftp://192.168.0.1:21</nowiki>"
 +
http_proxy="<nowiki>http://192.168.0.1:80</nowiki>"
 +
 
 +
* {{ic|MANPATH}} contains a colon-separated list of directories in which ''man'' searches for the man pages.
 +
{{Note|In {{ic|/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 unset. See {{man|5|manpath}}.
 +
}}
  
*{{ic|PWD}} contains the path to your working directory.
+
* {{ic|INFODIR}} contains a colon-separated list of directories in which the ''info'' command searches for the info pages, e.g., {{ic|/usr/share/info:/usr/local/share/info}}
  
*{{ic|OLDPWD}} contains the path to your previous working directory, that is, the value of {{ic|PWD}} before last ''cd'' was executed.
+
* {{ic|TZ}} can be used to to set a time zone different to the system zone for a user. The zones listed in {{ic|/usr/share/zoneinfo/}} can be used as reference, for example {{ic|1=TZ=":/usr/share/zoneinfo/Pacific/Fiji"}}. When pointing the {{ic|TZ}} variable to a zoneinfo file, it should start with a colon per [https://www.gnu.org/software/libc/manual/html_node/TZ-Variable.html the GNU manual].
  
*{{ic|SHELL}} contains the name of the running, interactive shell, e.g., {{ic|bash}}
+
=== Default programs ===
  
*{{ic|TERM}} contains the name of the running terminal, e.g., {{ic|xterm}}
+
* {{ic|SHELL}} contains the path to the user's [http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap08.html#tag_08_03 preferred shell]. Note that this is not necessarily the shell that is currently running, although [[Bash]] sets this variable on startup.
  
*{{ic|PAGER}} contains command to run the program used to list the contents of files, e.g., {{ic|/bin/less}}.
+
* {{ic|PAGER}} contains command to run the program used to list the contents of files, e.g., {{ic|/bin/less}}.
  
*{{ic|EDITOR}} contains the command to run the lightweight program used for editing files, e.g., {{ic|/usr/bin/nano}}. For example, you can write an interactive switch between ''gedit'' under [[X]] or ''nano'' in this example):
+
* {{ic|EDITOR}} contains the command to run the lightweight program used for editing files, e.g., {{ic|/usr/bin/nano}}. For example, you can write an interactive switch between ''gedit'' under [[X]] or ''nano'', in this example:
  
 
  export EDITOR="$(if <nowiki>[[</nowiki> -n $DISPLAY <nowiki>]]</nowiki>; then echo 'gedit'; else echo 'nano'; fi)"
 
  export EDITOR="$(if <nowiki>[[</nowiki> -n $DISPLAY <nowiki>]]</nowiki>; then echo 'gedit'; else echo 'nano'; fi)"
  
*{{ic|VISUAL}} contains command to run the full-fledged editor that is used for more demanding tasks, such as editing mail (e.g., {{ic|vi}}, [[vim]], [[emacs]] etc).
+
* {{ic|VISUAL}} contains command to run the full-fledged editor that is used for more demanding tasks, such as editing mail (e.g., {{ic|vi}}, [[vim]], [[emacs]] etc).
 
 
*{{ic|MAIL}} contains the location of incoming email. The traditional setting is {{ic|/var/spool/mail/$LOGNAME}}.
 
  
*{{ic|BROWSER}} 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]]:
+
* {{ic|BROWSER}} 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 <nowiki>[</nowiki> -n "$DISPLAY" <nowiki>]</nowiki>; then
 
  if <nowiki>[</nowiki> -n "$DISPLAY" <nowiki>]</nowiki>; then
Line 129: Line 157:
 
     export BROWSER=links
 
     export BROWSER=links
 
  fi
 
  fi
 
*{{ic|ftp_proxy and http_proxy}} contains FTP and HTTP proxy server, respectively:
 
ftp_proxy="<nowiki>ftp://192.168.0.1:21</nowiki>"
 
http_proxy="<nowiki>http://192.168.0.1:80</nowiki>"
 
 
*{{ic|MANPATH}} contains a colon-separated list of directories in which ''man'' searches for the man pages.
 
{{Note|In {{ic|/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. {{ic|/usr/share/man:/usr/local/share/man}}
 
}}
 
 
*{{ic|INFODIR}} contains a colon-separated list of directories in which the info command searches for the info pages, e.g., {{ic|/usr/share/info:/usr/local/share/info}}
 
 
*{{ic|TZ}} can be used to to set a time zone different to the system zone for a user. The zones listed in {{ic|/usr/share/zoneinfo/}} can be used as reference, for example {{ic|1=TZ="/usr/share/zoneinfo/Pacific/Fiji"}}
 
  
 
=== Using pam_env ===
 
=== Using pam_env ===
  
Using {{ic|/etc/environment}} and {{ic|~/.pam_environment}} can be a little tricky, and the man pages ({{ic|pam_env(8)}} and {{ic|pam_env.conf(5)}}) are not particularly clear. So, here's an example:
+
The [[PAM]] module {{man|8|pam_env}} loads the variables to be set in the environment from the following files: {{ic|/etc/security/pam_env.conf}}, {{ic|/etc/environment}} and {{ic|~/.pam_environment}}.
 
 
{{hc|1=~/.pam_environment|2=
 
LANG            DEFAULT=en_US.UTF-8
 
LC_ALL          DEFAULT=${LANG}
 
 
 
XDG_CONFIG_HOME  DEFAULT=@{HOME}/.config
 
#XDG_CONFIG_HOME=@{HOME}/.config                    # is **not** valid see below
 
XDG_DATA_HOME    DEFAULT=@{HOME}/.local/share
 
 
# you can even use recently defined variables
 
RCRC            DEFAULT=${XDG_CONFIG_HOME}/rcrc
 
BROWSER=firefox
 
#BROWSER        DEFAULT=firefox # same as above
 
EDITOR=vim}}
 
 
 
In {{ic|~/.pam_environment}} there are two ways to set environmental variables:
 
 
 
VARIABLE=VALUE
 
 
 
and
 
 
 
VARIABLE [DEFAULT=[value]] [OVERRIDE=[value]]
 
  
The first one '''doesn't allow''' the use of {{ic| ${VARIABLES} }}, while the second does. {{ic| @{HOME} }} is a special variable that expands what is defined in {{ic|/etc/passwd}} (same goes with {{ic| @{SHELL} }}). After defining a {{ic|VARIABLE}}, you can recall it with {{ic| ${VARIABLE} }}. Note that curly braces and the dollar sign are needed ({{ic| ${} }}) when invoking the previously defined variable.
+
* {{ic|/etc/environment}} must consist of simple {{ic|1=''VARIABLE''=''value''}} pairs on separate lines, for example: {{bc|1=EDITOR=nano}}
 +
* {{ic|/etc/security/pam_env.conf}} and {{ic|~/.pam_environment}} share the same following format: {{bc|1=VARIABLE [DEFAULT=''value''] [OVERRIDE=''value'']}} {{ic|@{HOME} }} and  {{ic|@{SHELL} }} are special variables that expand to what is defined in {{ic|/etc/passwd}}. The following example illustrates how to expand the {{ic|HOME}} environment variable into another variable: {{bc|1=XDG_CONFIG_HOME  DEFAULT=@{HOME}/.config}} {{Note|The variables {{ic|${HOME} }} and {{ic|${SHELL} }} are not linked to the {{ic|HOME}} and {{ic|SHELL}} environment variables, they are not set by default.}} The format also allows to expand already defined variables in the values of other variables using {{ic|${''VARIABLE''} }}, like this: {{bc|1=GNUPGHOME        DEFAULT=${XDG_CONFIG_HOME}/gnupg}} {{ic|1=''VARIABLE''=''value''}} pairs are also allowed, but variable expansion is not supported in those pairs. See {{man|5|pam_env.conf}} for more information.  
  
{{Note| This file is read before everything, even {{ic| ~/.{,bash_,z}profile }} and {{ic| ~/.zshenv }}. }}
+
{{Note|These files are read before other files, in particular before  {{ic|~/.profile}}, {{ic|~/.bash_profile}} and {{ic|~/.zshenv}}. }}
  
 
== See also ==
 
== See also ==
  
 
* [https://wiki.gentoo.org/wiki/Handbook:X86/Working/EnvVar Gentoo Linux Documentation]
 
* [https://wiki.gentoo.org/wiki/Handbook:X86/Working/EnvVar Gentoo Linux Documentation]
 +
* [https://help.ubuntu.com/community/EnvironmentVariables Ubuntu Community Wiki - Environment Variables]

Latest revision as of 13:38, 8 November 2019

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 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 (\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 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.

  • /etc/environment is used by the pam_env module and is shell agnostic so scripting or glob expansion cannot be used. The file only accepts variable=value pairs. See pam_env(8) and pam_env.conf(5) for details.
  • Global configuration files of your shell, initializes variables and runs scripts. For example Bash#Configuration files or Zsh#Startup/Shutdown files.
  • /etc/profile initializes variables for login shells only. It does, however, run scripts and can be used by all Bourne shell compatible shells.

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 environment

Environment variables for Xorg applications can be set in xinitrc, or in xprofile when using a display manager, for example:

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

Applications running on Wayland may use systemd user environment variables instead, as Wayland does not initiate any Xorg related files:

~/.config/environment.d/envvars.conf
PATH=$PATH:~/scripts
GUIVAR=value

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.

Tip: KDE Plasma supports executing shell scripts at login and even before launching Plasma, they can be used to set environment variables. See KDE#Autostart.

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.

  • DE indicates the Desktop Environment being used. xdg-open will use it to choose more user-friendly file-opener application that desktop environment provides. Some packages need to be installed to use this feature. For GNOME, that would be libgnomeAUR; for Xfce this is exo. Recognised values of DE variable are: gnome, kde, xfce, lxde and mate.
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.
  • DESKTOP_SESSION is similar to DE, but used in LXDE desktop environment: when DESKTOP_SESSION is set to LXDE, xdg-open will use pcmanfm file associations.
  • PATH 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 PATH, a relative or absolute path to the executable must be given, e.g. ./a.out or /bin/ls.
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.
  • HOME 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.
  • PWD contains the path to your working directory.
  • OLDPWD contains the path to your previous working directory, that is, the value of PWD before last cd was executed.
  • TERM contains the type of the running terminal, e.g. xterm-256color. It is used by programs running in the terminal that wish to use terminal-specific capabilities.
  • MAIL contains the location of incoming email. The traditional setting is /var/spool/mail/$LOGNAME.
  • ftp_proxy and http_proxy contains FTP and HTTP proxy server, respectively:
ftp_proxy="ftp://192.168.0.1:21"
http_proxy="http://192.168.0.1:80"
  • MANPATH contains a colon-separated list of directories in which man searches for the man pages.
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 unset. See manpath(5).
  • INFODIR contains a colon-separated list of directories in which the info command searches for the info pages, e.g., /usr/share/info:/usr/local/share/info
  • TZ can be used to to set a time zone different to the system zone for a user. The zones listed in /usr/share/zoneinfo/ can be used as reference, for example TZ=":/usr/share/zoneinfo/Pacific/Fiji". When pointing the TZ variable to a zoneinfo file, it should start with a colon per the GNU manual.

Default programs

  • SHELL 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.
  • PAGER contains command to run the program used to list the contents of files, e.g., /bin/less.
  • EDITOR contains 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)"
  • VISUAL contains command to run the full-fledged editor that is used for more demanding tasks, such as editing mail (e.g., vi, vim, emacs etc).
  • BROWSER 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

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 and ~/.pam_environment.

  • /etc/environment must consist of simple VARIABLE=value pairs on separate lines, for example:
    EDITOR=nano
  • /etc/security/pam_env.conf and ~/.pam_environment share the same following format:
    VARIABLE [DEFAULT=value] [OVERRIDE=value]
    @{HOME} and @{SHELL} are special variables that expand to what is defined in /etc/passwd. The following example illustrates how to expand the HOME environment variable into another variable:
    XDG_CONFIG_HOME   DEFAULT=@{HOME}/.config
    Note: The variables ${HOME} and ${SHELL} are not linked to the HOME and SHELL environment variables, they are not set by default.
    The format also allows to expand already defined variables in the values of other variables using ${VARIABLE} , like this:
    GNUPGHOME        DEFAULT=${XDG_CONFIG_HOME}/gnupg
    VARIABLE=value pairs are also allowed, but variable expansion is not supported in those pairs. See pam_env.conf(5) for more information.
Note: These files are read before other files, in particular before ~/.profile, ~/.bash_profile and ~/.zshenv.

See also