Environment variables

From ArchWiki
Revision as of 19:42, 6 October 2011 by Roygbiv (Talk | contribs) (Defining Variables Locally: example fix)

Jump to: navigation, search

This template has only maintenance purposes. For linking to local translations please use interlanguage links, see Help:i18n#Interlanguage links.


Local languages: Català – Dansk – English – Español – Esperanto – Hrvatski – Indonesia – Italiano – Lietuviškai – Magyar – Nederlands – Norsk Bokmål – Polski – Português – Slovenský – Česky – Ελληνικά – Български – Русский – Српски – Українська – עברית – العربية – ไทย – 日本語 – 正體中文 – 简体中文 – 한국어


External languages (all articles in these languages should be moved to the external wiki): Deutsch – Français – Română – Suomi – Svenska – Tiếng Việt – Türkçe – فارسی

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 filesystem, 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 provides a simple way to share configuration settings between multiple applications and processes in Linux.

Utilities

The Template:Package Official package contains printenv and env. To list the current environmental variables, use printenv to print the names and the values of each. Note that some environment variables are user-specific - check by comparing the printenv output as root:

$ printenv

The env utility can be used to run a command under a modified environment. In the simplest case:

$ env EDITOR=vim xterm

will set the default editor to vim in the new xterm session. This will not affect the EDITOR outside this session.

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: gnu.org.

Examples

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

  • PATH Contains a colon-separated list of directories in which your system looks for executable files. When a regular command (i.e. ls, rc-update or emerge) is interpreted by the shell (i.e. bash, zsh), the shell looks for an executable file with same name as your command in the listed directories, and executes it. To run executables that are not listed in PATH, the absoute path to the executable must be given: /bin/ls.
  • HOME Contains the path to the home directory of the current user. This variable can be used by applications to associate configuration files and suchlike with the user running it.
  • EDITOR Contains the path to the program used for editing files, i.e. /usr/bin/nano.
  • SHELL Contains the name of the running, interactive shell, i.e bash
  • TERM Contains the name of the running terminal, i.e xterm
  • 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.
  • MANPATH Contains a colon-separated list of directories in which man searches for the man pages. Note that in Template:Filename, 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
  • INFODIR Contains a colon-separated list of directories in which the info command searches for the info pages, i.e. /usr/share/info:/usr/local/share/info
  • PAGER Contains the path to the program used to list the contents of files, i.e. /bin/more.

Defining Variables Globally

Most Linux distributions tell you to change or add environment variable definitions in /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. The following files should be used for defining global environment variables on your system: /etc/profile, /etc/bash.bashrc and /etc/environment.

Defining Variables Locally

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. The following files should be used for local environment variables on your system: ~/.bashrc, ~/.profile, ~/.bash_login and ~/.bash_logout.

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

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

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

Session Specific Variables

Sometimes even stricter definitions are required. You might want to be able to use binaries from a temporary directory you created without using the path to the binaries themselves or editing Template:Filename for the short time you need it.

In this case, you can just define the PATH variable in your current session by using the export command. As long as you do not log out, the PATH variable will be using the temporary settings.

Example: Defining a session-specific environment variable

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


References

Gentoo Linux Documentation [1]