Environment variable

来源:互联网 发布:qq三国79js奥义三板斧 编辑:程序博客网 时间:2024/04/26 06:58
参考:
1.https://en.wikipedia.org/wiki/Environment_variable
2.http://unix.stackexchange.com/questions/117467/how-to-permanently-set-environmental-variables
环境变量定义、概述
     Environment variables are a set of dynamic named values that can affect the way running processes will behave on a computer.
     They are part of the environment in which a process runs. For example, a running process can query the value of the TEMP environment variable to discover a suitable location to store temporary files, or the HOME or USERPROFILE variable to find the directory structure owned by the user running the process.
     In all Unix and Unix-like systems, each process has its own separate set of environment variables. By default, when a process is created, it inherits a duplicate environment of its parent process, except for explicit changes made by the parent when it creates the child. At the API level, these changes must be done between running fork and exec. Alternatively, from command shells such as bash, a user can change environment variables for a particular command invocation by indirectly invoking it viaenv or using the ENVIRONMENT_VARIABLE=VALUE <command> notation. AllUnix operating system flavors, DOS, and Windows have environment variables; however, they do not all use the same variable names. A running program can access the values of environment variables for configuration purposes.

常见的环境变量
Examples of environment variables include:
  • PATH - a list of directory paths. When the user types a command without providing the full path, this list is checked to see whether it contains a path that leads to the command.
  • HOME (Unix-like) and USERPROFILE (Microsoft Windows) - indicate where a user's home directory is located in the file system.
  • HOME/{.AppName} (Unix-like) and APPDATA\{DeveloperName\AppName} (Microsoft Windows) - for storing application settings. Many applications incorrectly use USERPROFILE for application settings in Windows - USERPROFILE should only be used in dialogs that allow user to choose between paths like Documents/Pictures/Downloads/Music, for programmatic purposes APPDATA (roaming), LOCALAPPDATA or PROGRAMDATA (shared between users) is used.
  • TERM (Unix-like) - specifies the type of computer terminal or terminal emulator being used (e.g., vt100 or dumb).
  • PS1 (Unix-like) - specifies how the prompt is displayed in the Bourne shelland variants.
  • MAIL (Unix-like) - used to indicate where a user's mail is to be found.
  • TEMP - location where processes can store temporary files


环境变量的作用
Shell scripts and batch files use environment variables to communicate data and preferences to child processes. They can also be used to store temporary values for reference later in a shell script. However, in Unix, other variables are usually used for this.

Unix和Window中,各自改变某个环境变量产生的不同影响
     In Unix, an environment variable that is changed in a script or compiled program will only affect that process and possibly child processes. The parent process and any unrelated processes will not be affected. In MS-DOS, changing or removing a variable's value inside a batch file will change the variable for the duration of COMMAND.COM's existence.


环境变量初始化的时机
In Unix, the environment variables are normally initialized during system startup by the system init scripts, and hence inherited by all other processes in the system. Users can, and often do, augment them in the profile script for the command shell they are using. In Microsoft Windows, each environment variable's default value is stored in the Windows registry or set in theAUTOEXEC.BAT file.

On Unix, a setuid program is given an environment chosen by its caller, but it runs with different authority from its caller. Thedynamic linker will usually load code from locations specified by the environment variables $LD_LIBRARY_PATH and$LD_PRELOAD and run it with the process's authority. If a setuid program did this, it would be insecure, because its caller could get it to run arbitrary code and hence misuse its authority. For this reason, libc unsets these environment variables at startup in a setuid process. setuid programs usually unset unknown environment variables and check others or set them to reasonable values.


环境变量的显示
The variables can be used both in scripts and on the command line. They are usually referenced by putting special symbols in front of or around the variable name. For instance, to display the user home directory, in most scripting environments, the user has to type:
echo $HOME

In DOS, OS/2 and Windows command-line interpreters such as COMMAND.COM andcmd.exe, the user has to type this:

ECHO %HOME%

In Windows PowerShell, the user has to type this:

Write-Output $env:HOMEPATH

The commands envset, and printenvdisplay all environment variables and their values. printenv can also be used to print a single variable by giving that variable name as the sole argument to the command.



环境变量的设置
The commands env and set are also used to set environment variables and are often incorporated directly into the shell.

Unix[edit]

In Unix, the following commands can also be used, but are often dependent on a certain shell.

VARIABLE=value         #export VARIABLE        # for Bourne and related shells
export VARIABLE=value  # for ksh, bash, and related shells
setenv VARIABLE value  # for csh and related shells

A few simple principles govern how environment variables achieve their effect.

Shell进程之间的环境变量是独立的
     Environment variables are local to the process in which they were set. If two s
hell processes are spawned and the value of an environment variable is changed in one, that change will not be seen by the other.

程序之间调用时,环境变量的情况
When a child process is created, it inherits all the environment variables and their values from the parent process. Usually, when a program calls another program, it first creates a child process by forking, then the child adjusts the environment as needed and lastly the child replaces itself with the program to be called. This procedure gives the calling program control over the environment of the called program.

In Unix and Unix-like systems, the names of environment variables are case-
sensitive.

需要注意环境变量的设置有时侯只是临时的
In Unix shells, variables may be assigned without the export keyword. Variables defined in this way are displayed by theset command, but are not true environment variables, as they are stored only by the shell and not recognized by the kernel. The printenv command will not display them, and child processes do not inherit them.
VARIABLE=value

However, if used in front of a program to run, the variables will be exported to the environment and thus appear as real environment variables to the program:

VARIABLE=value program_name [arguments]

The persistence of an environment variable can be session-wide or system-wide.
如何保证环境变量的永久性
You can add it to the file .profile or .bashrc or your current shell profile file (located in your home directory). Then, each time you open your shell it will be loaded.

To change the environmental variable "permanently" you'll need to consider at least these situations:

  1. Login/Non-login shell
  2. Interactive/Non-interactive shell

bash

  1. Bash as login shell will load /etc/profile~/.bash_profile~/.bash_login~/.profile in the order
  2. Bash as non-login interactive shell will load ~/.bashrc
  3. Bash as non-login non-interactive shell will load the configuration specified in environment variable $BASH_ENV
$EDITOR ~/.bashrc#add lines at the bottom of the file:       export LD_LIBRARY_PATH=/usr/lib/oracle/11.2/client64/lib     export ORACLE_HOME=/usr/lib/oracle/11.2/client64

zsh

$EDITOR ~/.zshrc#add lines at the bottom of the file:       export LD_LIBRARY_PATH=/usr/lib/oracle/11.2/client64/lib     export ORACLE_HOME=/usr/lib/oracle/11.2/client64

ksh

$EDITOR ~/.profile#add lines at the bottom of the file:       export LD_LIBRARY_PATH=/usr/lib/oracle/11.2/client64/lib     export ORACLE_HOME=/usr/lib/oracle/11.2/client64

bourne

$EDITOR ~/.profile#add lines at the bottom of the file:       LD_LIBRARY_PATH=/usr/lib/oracle/11.2/client64/lib          ORACLE_HOME=/usr/lib/oracle/11.2/client64     export LD_LIBRARY_PATH ORACLE_HOME

csh or tcsh

$EDITOR ~/.login#add lines at the bottom of the file:       setenv LD_LIBRARY_PATH /usr/lib/oracle/11.2/client64/lib     setenv ORACLE_HOME /usr/lib/oracle/11.2/client64

If you want to make it permanent for all users, you can edit /etc/profile or /etc/environment.
In this case follow the syntax you see already present in your file.

DOS, OS/2 and Windows[edit]

In DOS, OS/2 and Windows command-line interpreters such as COMMAND.COM andcmd.exe, the SET command is used to assign environment variables and values using the following arguments:

SET VARIABLE=value

The SET command without any arguments displays all environment variables along with their values. 
0 0