Environment variables (简体中文)

From ArchWiki
翻译状态:本文是 Environment_variables翻译。上次翻译日期:2016-09-07。如果英文版本有所更改,则您可以帮助同步翻译。

环境变量是一个有名称的对象,包含可被其它程序使用的数据。简单的说,它是一个名称和数值对。环境变量的值可以是文件系统上所有执行程序的位置,默认的编辑器,系统本地化设置等。Linux 新用户可能觉得这种管理变量的方式有点混乱。但是环境变量提供了一种在多个程序和进程间共享配置的方式。

工具

coreutils软件包含程序printenvenv. 要显示当前环境变量的值:

$ printenv
注意: 一些环境变量是属于特定用户的. 比较一般用户和root用户的 printenv 就可以看到差异。

工具 env 可以用指定的环境变量执行一个命令。下面例子把环境变量 EDITOR 设置为 vim 然后执行命令 xterm. 这个操作不会影响全局环境变量EDITOR.

$ env EDITOR=vim xterm

Bash 内建的 set 命令可以设置 shell 选项值或显示 shell 变量的名称和数值。更多信息请参阅: [1].

每个进程都把他们的环境变量保存在 /proc/$PID/environ 文件中。此文件包含用 nul (\x0) 字符分隔的键值对。用 sed 可以获得用户可读的内容:sed 's:\x0:\n:g' /proc/$PID/environ.

定义变量

全局

理论上,任何 shell 脚本都可以初始化环境变量,但为了维护方便,环境变量的定义集中在几个特定的文件中。对全局变量来说是:/etc/profile, /etc/bash.bashrc/etc/environment. 每个文件都有不同的限制,请根据需要选择要使用的文件。

  • /etc/profile 初始化登陆 shell 的环境变量。它可以执行脚本并支持 Bash 兼容 Shell。
  • /etc/bash.bashrc 初始化交互 shell,它也可以执行脚本但是只支持 Bash。
  • /etc/environment 被 PAM-env 模块使用,和登陆与否,交互与否,Bash与否无关,所以无法使用脚本或通配符展开。仅接受 variable=value 格式。

以将 ~/bin 加入某些特定用户的 PATH 为例,可以将其放入 /etc/profile/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=$HOME/bin:${PATH}
fi

按用户

注意: dbus 进程和 systemd 用户实例不会使用任何 .bashrc 等文件定义的环境变量。所以 dbus 启动的程序默认不会使用这些变量,请参考 Systemd/User#Environment variables.

有时并不希望定义全局环境变量,比如要把 /home/my_user/bin 加入 PATH 变量但是不影响其它用户。本地环境变量可以在下面文件定义:

  1. shell 配置文件,例如 Bash#Configuration filesZsh#Startup/Shutdown files.
  2. 很多 shell 会使用 ~/.profile 作为后备方案参考 wikipedia:Unix shell#Configuration files.
  3. ~/.pam_environment 是用户特有的环境变量,PAM-env 模块会使用它。参考 pam_env(8)pam_env.conf(5)

要修改本地用户的路径变量,修改 ~/.bash_profile:

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

要更新变量,重新登录或 source 文件 $ source ~/.bash_profile.

图形程序

要设置图形程序的环境变量,可以将变量放入 xinitrc (从 显示管理器 登陆时,使用 xprofile),例如:

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

按会话

如果需要更严格的定义,例如在运行程序时临时修改路径,在短时间改变 ~/.bash_profile 等。这时,可以用 export 命令在当前会话修改 PATH,只要不退出登录,PATH 变量就会一直生效。增加 PATH 到一个会话:

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

示例

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 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 (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 PATH, the absoute path to the executable must be given: /bin/ls.
注意: 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, e.g., bash
  • TERM contains the name of the running terminal, e.g., xterm
  • 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).
  • 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.
注意: 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, 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"

使用 pam_env

Using /etc/environment and ~/.pam_environment can be a little tricky, and the man pages (pam_env(8) and pam_env.conf(5)) are not particularly clear. So, here is an example:

~/.pam_environment
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 ~/.pam_environment there are two ways to set environmental variables:

VARIABLE=VALUE

and

VARIABLE [DEFAULT=[value]] [OVERRIDE=[value]]

The first one does not allow the use of ${VARIABLES} , while the second does. @{HOME} is a special variable that expands what is defined in /etc/passwd (same goes with @{SHELL} ). After defining a VARIABLE, you can recall it with ${VARIABLE} . Note that curly braces and the dollar sign are needed ( ${} ) when invoking the previously defined variable.

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

参阅