Handbook:AMD64/Working/EnvVar/pt-br
Variáveis de ambiente
Introdução
Uma variável de ambiente é um objeto com nome que contém informações utilizadas por uma ou mais aplicações. Usando variáveis de ambiente pode-se facilmente alterar configurações para uma ou mais aplicações.
Exemplos Importantes
A tabela a seguir lista algumas das variáveis utilizadas por um sistema Linux e descreve seus usos. Exemplos de valores são apresentados após a tabela.
Variable | Description |
---|---|
PATH | This variable contains a colon-separated list of directories in which the system looks for executable files. If a name is entered of an executable (such as ls, rc-update, or emerge) but this executable is not located in a listed directory, then the system will not execute it (unless the full path is entered as the command, such as /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. |
LDPATH | This variable contains a colon-separated list of directories in which the dynamical linker searches through to find a library. |
MANPATH | This variable contains a colon-separated list of directories in which the man command searches for the man pages. |
INFODIR | This variable contains a colon-separated list of directories in which the info command searches for the info pages. |
PAGER | This variable contains the path to the program used to list the contents of files through (such as less or more). |
EDITOR | This variable contains the path to the program used to change the contents of files with (such as nano or vi). |
KDEDIRS | This variable contains a colon-separated list of directories which contain KDE-specific material. |
CONFIG_PROTECT | This variable contains a space-delimited list of directories which should be protected by Portage during package updates. |
CONFIG_PROTECT_MASK | This variable contains a space-delimited list of directories which should not be protected by Portage during package updates. |
Below is an example definition of all these variables:
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"
LDPATH="/lib:/usr/lib:/usr/local/lib:/usr/lib/gcc-lib/i686-pc-linux-gnu/3.2.3"
MANPATH="/usr/share/man:/usr/local/share/man"
INFODIR="/usr/share/info:/usr/local/share/info"
PAGER="/usr/bin/less"
EDITOR="/usr/bin/vim"
KDEDIRS="/usr"
# Directories that are protected during package updates.
# Note the use of the \ (backslashes) on the end of the following lines which interprets to a single space-delimited line.
CONFIG_PROTECT="/usr/X11R6/lib/X11/xkb /opt/tomcat/conf \
/usr/kde/3.1/share/config /usr/share/texmf/tex/generic/config/ \
/usr/share/texmf/tex/platex/config/ /usr/share/config"
# Directories that are _not_ protected during package updates.
CONFIG_PROTECT_MASK="/etc/gconf"
Defining variables globally
O diretório env.d
To centralize the definitions of these variables, Gentoo introduced the /etc/env.d/ directory. Inside this directory a number of files are available, such as 50baselayout, gcc/config-x86_64-pc-linux-gnu, etc. which contain the variables needed by the application mentioned in their name.
For instance, when gcc is installed, a file called gcc/config-x86_64-pc-linux-gnu was created by the ebuild which contains the definitions of the following variables:
GCC_PATH="/usr/x86_64-pc-linux-gnu/gcc-bin/13"
LDPATH="/usr/lib/gcc/x86_64-pc-linux-gnu/13:/usr/lib/gcc/x86_64-pc-linux-gnu/13/32"
MANPATH="/usr/share/gcc-data/x86_64-pc-linux-gnu/13/man"
INFOPATH="/usr/share/gcc-data/x86_64-pc-linux-gnu/13/info"
STDCXX_INCDIR="g++-v13"
CTARGET="x86_64-pc-linux-gnu"
GCC_SPECS=""
MULTIOSDIRS="../lib64:../lib"
Other distributions might tell the system administrator to change or add such environment variable definitions in /etc/profile or other locations. Gentoo on the other hand makes it easy for the sysadmins (and for Portage) to maintain and manage the environment variables without having to pay attention to the numerous files that can contain environment variables.
For instance, when gcc is updated, the associated file(s) under /etc/env.d/gcc are updated too without requesting any administrative interaction.
There is still the occasionally where a system administrator is asked to set a certain environment variable system-wide. As an example, take the http_proxy variable. Instead of editing a file under the /etc/profile directory, create a file named /etc/env.d/99local and enter the definition in it:
http_proxy="proxy.server.com:8080"
By using the same file for all customized environment variables, system administrators have a quick overview on the variables they have defined themselves.
env-update
Several files within the /etc/env.d directory add definitions to the PATH variable. This is not a mistake: when the env-update command is executed, it will append the several definitions before it atomically updates each environment variable, thereby making it easy for packages (or system administrators) to add their own environment variable settings without interfering with the already existing values.
The env-update script will append the values in the alphabetical order of the /etc/env.d/ files. The file names must begin with two decimal digits.
09sandbox 50baselayout 51dconf
+------------+----------------+-----------+
CONFIG_PROTECT_MASK="/etc/sandbox.d /etc/gentoo-release /etc/dconf ..."
The concatenation of variables does not always happen, only with the following variables: ADA_INCLUDE_PATH, ADA_OBJECTS_PATH, CLASSPATH, KDEDIRS, PATH, LDPATH, MANPATH, INFODIR, INFOPATH, ROOTPATH, CONFIG_PROTECT, CONFIG_PROTECT_MASK, PRELINK_PATH, PRELINK_PATH_MASK, PKG_CONFIG_PATH, and PYTHONPATH. For all other variables the latest defined value (in alphabetical order of the files in /etc/env.d/) is used.
It is possible to add more variables into this list of concatenate-variables by adding the variable name to either COLON_SEPARATED or SPACE_SEPARATED variables (also inside an /etc/env.d/ file).
When executing env-update, the script will create all environment variables and place them in /etc/profile.env (which is used by /etc/profile). It will also extract the information from the LDPATH variable and use that to create /etc/ld.so.conf. After this, it will run ldconfig to recreate the /etc/ld.so.cache file used by the dynamical linker.
To notice the effect of env-update immediately after running it, execute the following command to update the environment. Users who have installed Gentoo themselves will probably remember this from the installation instructions:
root #
env-update && source /etc/profile
The above command only updates the variables in the current terminal, new consoles, and their children. Thus, if the user is working in X11, he needs to either type source /etc/profile in every new terminal opened or restart X so that all new terminals source the new variables. If a login manager is used, it is necessary to become root and restart the /etc/init.d/xdm service.
It is not possible to use shell variables when defining other variables. This means things like
FOO="$BAR"
(where $BAR is another variable) are forbidden.Defining variables locally
User specific
It might not be necessary to define an environment variable globally. For instance, one might want to add /home/my_user/bin and the current working directory (the directory the user is in) to the PATH variable but do not want all other users on the system to have that in their PATH too. To define an environment variable locally, use ~/.bashrc or ~/.bash_profile:
# A colon followed by no directory is treated as the current working directory
PATH="${PATH}:/home/my_user/bin:"
After logout/login, the PATH variable will be updated.
Session specific
Sometimes even stricter definitions are requested. For instance, a user might want to be able to use binaries from a temporary directory created without using the path to the binaries themselves or editing ~/.bashrc for the short time necessary.
In this case, just define the PATH variable in the current session by using the export command. As long as the user does not log out, the PATH variable will be using the temporary settings.
root #
export PATH="${PATH}:/home/my_user/tmp/usr/bin"