Difference between revisions of "Environment variables"

From ArchWiki
Jump to: navigation, search
m (Defining Variables Locally: put the PATH= example in a code box)
(Examples)
(27 intermediate revisions by 12 users not shown)
Line 1: Line 1:
[[Category:Command shells (English)]]
+
[[Category:System administration]]
{{i18n|Environment Variables}}
+
[[de:Umgebungsvariablen]]
 
+
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.
An environment variable is a named object that contains information used by one or more applications. Many users (and especially those new to Linux) find this a bit weird or unmanageable. However, this is a mistake: by using environment variables one can easily change a configuration setting for one or more applications.  
+
  
 
==Utilities==
 
==Utilities==
The {{Package Official|coreutils}} package contains {{Codeline|printenv}} and {{Codeline|env}}.
+
The {{Pkg|coreutils}} package contains {{ic|printenv}} and {{ic|env}}. To list the current environmental variables, use {{ic|printenv}} to print the names and the values of each. Note that some environment variables are user-specific - check by comparing the {{ic|printenv}} output as root:
 
+
Use {{Codeline|printenv}} to print the names and values of current environment variables:
+
  
 
  $ printenv
 
  $ printenv
  
{{note|Some environment variables are user-specific. Check by comparing the {{Codeline|printenv}} output as root.}}
+
The {{ic|env}} utility can be used to run a command under a modified environment. In the simplest case:
  
The {{Codeline|env}} utility can be used to run a command under a modified environment. In the simplest case:
+
$ env EDITOR=vim xterm
  
$ env [NAME=VALUE] [COMMAND]
+
will set the default editor to vim in the new xterm session. This will not affect the {{ic|EDITOR}} outside this session.
  
The [[Bash]] builtin {{Codeline|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"''[http://www.gnu.org/software/bash/manual/bash.html#The-Set-Builtin ]:
+
The [[Bash]] builtin {{ic|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 documentation] on  {{ic|set}} built-in command.
  
 
==Examples==
 
==Examples==
The following table lists a number of variables used by a Linux system and describes their use.
+
The following section lists a number of common environment variables used by a Linux system and describes their values.
  
----
+
*{{ic|DE}} indicate the '''D'''esktop '''E'''nvironment being used. [[xdg-open]] will use it to chose 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, 'exo'. Recognised values of {{ic|DE}} variable are: '''gnome''', '''kde''', '''xfce''', '''lxde''' and '''mate'''.
'''PATH''' This variable contains a colon-separated list of directories in which your system looks for executable files. If you enter a name of an executable (such as ls, rc-update or emerge) but this executable is not located in a listed directory, your system will not execute it (unless you enter the full path as a command, such as {{Codeline|/bin/ls}}).
+
  
'''ROOTPATH'''  This variable has the same function as PATH, but this one only lists the directories that should be checked when the root-user enters a command.
+
The {{ic|$DE}} environment variable needs to be exported before starting the window manager. For example:
 +
{{hc|~/.xinitrc|<nowiki>
 +
export DE="xfce"
 +
exec openbox
 +
</nowiki>}}
  
'''LDPATH'''    This variable contains a colon-separated list of directories in which the dynamical linker searches through to find a library.
+
This will make xdg-open use the more user-friendly exo-open, because it assumes it is inside Xfce. Use exo-preferred-applications for configuring.
  
'''MANPATH''' This variable contains a colon-separated list of directories in which the man command searches for the man pages.
+
*{{ic|DESKTOP_SESSION}}. In [[LXDE]] desktop enviroment, when DESKTOP_SESSION is set to LXDE, xdg-open will use pcmanfm file associations.
  
{{Note|In {{Filename|/etc/profile}}, there is a comment that states "Man is much better than us at figuring this out" when referring to the '''MANPATH''' environment variable.}}
+
*{{ic|PATH}} Contains a colon-separated list of directories in which your system looks for executable files. When a regular command (i.e. {{ic|ls}}, {{ic|rc-update}} or {{ic|emerge}}) is interpreted by the shell (i.e. {{ic|bash}}, {{ic|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 {{ic|PATH}}, the absoute path to the executable must be given: {{ic|/bin/ls}}.
  
'''INFODIR''' This variable contains a colon-separated list of directories in which the info command searches for the info pages.
+
{{Note|It is advised not to include the current working directory (.) into your {{ic|PATH}} for security reasons, as it may trick the user to execute vicious commands.}}
  
'''PAGER''' This variable contains the path to the program used to list the contents of files through (such as {{Package Official|less}} or more).
+
*{{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.
  
'''EDITOR''' This variable contains the path to the program used to change the contents of files with (such as {{Package Official|nano}}, {{Package Official|vi}}, or {{Package Official|vim}}).
+
*{{ic|PWD}} Contains the path to your working directory.
  
'''KDEDIRS''' This variable contains a colon-separated list of directories which contain [[KDE]]-specific material.
+
*{{ic|OLDPWD}} Contains the path to your previous working directory, that is, the value of PWD before last {{ic|cd}} was executed.
----
+
  
 +
*{{ic|SHELL}} Contains the name of the running, interactive shell, i.e {{ic|bash}}
  
The values for the above variables are defined in following table.
+
*{{ic|TERM}} Contains the name of the running terminal, i.e {{ic|xterm}}
  
----
+
*{{ic|PAGER}} Contains the path to the program used to list the contents of files, i.e. {{ic|/bin/less}}.
'''PATH'''="/bin:/usr/bin:/usr/local/bin:/opt/bin:/usr/games/bin"
+
  
'''ROOTPATH'''="/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin"
+
*{{ic|EDITOR}} Contains the path to the lightweight program used for editing files, i.e. {{ic|/usr/bin/nano}}, or an interactive switch (between gedit under X or nano in this example):
  
'''LDPATH'''="/lib:/usr/lib:/usr/local/lib:/usr/lib/gcc-lib/i686-pc-linux-gnu/3.2.3"
+
{{bc|<nowiki>export EDITOR="$(if [[ -n $DISPLAY ]]; then echo 'gedit'; else echo 'nano'; fi)"</nowiki>}}
  
'''MANPATH'''="/usr/share/man:/usr/local/share/man"
+
*{{ic|VISUAL}} Contains the path to full-fledged editor that is used for more demanding tasks, such as editing mail; e.g., {{ic|vi}}, [[vim]], [[emacs]], etc.
  
'''INFODIR'''="/usr/share/info:/usr/local/share/info"
+
*{{ic|MAIL}} Contains the location of incoming email. The traditional setting is {{ic|/var/spool/mail/$LOGNAME}}.
  
'''PAGER'''="/usr/bin/less"
+
*{{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]]:
  
'''EDITOR'''="/usr/bin/vim"
+
{{bc|<nowiki>if [ -n "$DISPLAY" ]; then
 +
export BROWSER=firefox
 +
else
 +
export BROWSER=links
 +
fi</nowiki>}}
  
'''KDEDIRS'''="/usr"
+
*{{ic|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"
 +
 
 +
*{{ic|MANPATH}} Contains a colon-separated list of directories in which {{ic|man}} searches for the man pages. Note that 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, i.e. {{ic|/usr/share/info:/usr/local/share/info}}
  
 
== Defining Variables Globally ==
 
== Defining Variables Globally ==
  
Most Linux distributions tell you to change or add environment variable definitions in {{Filename|/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. The following files may be present and used for global environment variables on your Arch Linux system: {{Filename|/etc/profile}}, {{Filename|/etc/bash.bashrc}}, {{Filename|/etc/environment}}
+
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. 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}}.
  
 
== Defining Variables Locally ==
 
== Defining Variables Locally ==
  
'''User Specific'''
+
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 PATH variable but do not want all other users on your system to have that in their {{ic|PATH}} too. The following files should be used for local environment variables on your system: {{ic|~/.bashrc}}, {{ic|~/.profile}}, {{ic|~/.bash_login}} and {{ic|~/.bash_logout}}.
  
You do not always want to define an environment variable globally. For instance, you might want to add {{Filename|/home/my_user/bin}} and the current working directory (the directory you are in) to the PATH variable but don't want all other users on your system to have that in their PATH too. The following files may be present and used for local environment variables on your Arch Linux system: {{Filename|~/.bashrc}}, {{Filename|~/.bash_profile}}, {{Filename|~/.profile}}, {{Filename|~/.bash_login}}, {{Filename|~/.bash_logout}}
+
To add a directory to {{ic|PATH}} for local usage, put following in {{ic|~/.bashrc}}:
  
'''Example:''' Extending PATH for local usage in {{Filename|~/.bashrc}} (''A colon followed by no directory is treated as the current working directory'')
+
PATH="${PATH}:/home/my_user/bin"
  
PATH="${PATH}:/home/my_user/bin:"
+
To update the variable, re-login or source the file: {{ic|$ source ~/.bashrc}}.
 
+
After the next time you log in, your PATH variable will be updated.
+
  
 
== Session Specific Variables ==
 
== Session Specific Variables ==
  
Sometimes even stricter definitions are requested. 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 ~/.bashrc for the short time you need it.
+
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|~/.bashrc}} for the short time needed to run them.
 
+
In this case, you can just define the PATH variable in your current session by using the export command. As long as you don't 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"
+
 
+
== A Users Example ==
+
 
+
To set the global environment variables for '''BROWSER''' and '''EDITOR''', I tried several files and was finally successful putting the following in the '''/etc/environment''' file.
+
 
+
'''BROWSER=firefox'''
+
 
+
'''EDITOR=nano'''
+
  
Be sure to re-login and check in a terminal to see if they are now set.
+
In this case, you can define the {{ic|PATH}} variable in your current session, combined with the {{ic|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:
  
additional info: http://pubs.opengroup.org/onlinepubs/009695399/basedefs/xbd_chap08.html
+
$ export PATH="${PATH}:/home/my_user/tmp/usr/bin"
  
==References==
+
==See also==
Gentoo Linux Documentation [http://www.gentoo.org/doc/en/handbook/handbook-x86.xml?full=1#book_part2_chap5]
+
*Gentoo Linux Documentation [http://www.gentoo.org/doc/en/handbook/handbook-x86.xml?full=1#book_part2_chap5]
 +
*[[Default Applications]]
 +
*[[Xdg-open]]

Revision as of 15:59, 28 August 2013

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 coreutils 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 documentation on set built-in command.

Examples

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

  • DE indicate the Desktop Environment being used. xdg-open will use it to chose 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 libgnome. For Xfce, '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 inside Xfce. Use exo-preferred-applications for configuring.

  • DESKTOP_SESSION. In LXDE desktop enviroment, 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 (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.
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.
  • SHELL Contains the name of the running, interactive shell, i.e bash
  • TERM Contains the name of the running terminal, i.e xterm
  • PAGER Contains the path to the program used to list the contents of files, i.e. /bin/less.
  • EDITOR Contains the path to the lightweight program used for editing files, i.e. /usr/bin/nano, or 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 the path to full-fledged editor that is used for more demanding tasks, such as editing mail; e.g., vi, vim, emacs, etc.
  • MAIL Contains the location of incoming email. The traditional setting is /var/spool/mail/$LOGNAME.
  • 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
  • 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 that 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
  • 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

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. One might want to temporarily run executables from a specific directory created without having to type the absolute path to each one, or editing ~/.bashrc 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"

See also