个人工具

UbuntuHelp:EnvironmentVariables

来自Ubuntu中文

Wikibot讨论 | 贡献2007年11月22日 (四) 12:32的版本 (新页面: {{From|https://help.ubuntu.com/community/EnvironmentVariables}} {{Languages|UbuntuHelp:EnvironmentVariables}} #title Environment Variables == Global Environment Variables == These may ...)

(差异) ←上一版本 | 最后版本 (差异) | 下一版本→ (差异)
跳转至: 导航, 搜索
  1. title Environment Variables


Global Environment Variables

These may be put in one of these files:

  • /etc/environment (try this file first)
  • /etc/profile

For example, to set a variable called JAVA_HOME and append it to PATH:

for /etc/environment:

JAVA_HOME="/usr/lib/j2sdk1.5-sun"

(A colon followed by no directory is treated as the current working directory)

PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/bin/X11:/usr/games:$JAVA_HOME:"

for /etc/profile:

export JAVA_HOME="/usr/lib/j2sdk1.5-sun"

export PATH="$PATH:$JAVA_HOME:"


Locale Environment variables

Some programs rely on the value of $LANG, which defaults to en_US.UTF-8. (For example, Thunderbird changes the displayed date format based on this). The variable is set in both /etc/enviroment and /etc/default/locale. To make a system-wide change that is certain to take effect, both of these must be modified; it's also necessary to log out and back in again.

Session Specific Environment Variables

You may sometimes want variables defined locally (eg.: restricted to your accounts shell only) and not globally (all users). So just append whatever variables you need defined to your ~/.bashrc file.

If instead you want a temporary variable created only for your current shell session, then do this inside the shell:

export SOMEDIRECTORY="/some/location"
export SOMEVALUE="500"

Bash Shell Environment Variables

  • When starting a shell after you have logged in already, either from the console or though an X display manager, that shell will be an interactive non-login shell, which will read /etc/bash.bashrc and ~/.bashrc.
  • When the user starts a bash shell by logging into a console or via SSH, this starts a login shell, which will read /etc/environment, /etc/profile, /etc/bash.bashrc, ~/.bash_profile, ~/.bash_login, and ~/.profile.
  • If Bash is invoked with the name sh, whether as an interactive login shell, or as a non-interactive shell with the --login option, it first attempts to read and execute commands from /etc/profile and ~/.profile, in that order.

Displaying contents of $PATH

In a shell type:

echo $PATH

References