When a shell is running, three main types of variables are present:
1. Local Variables
A local variable is a variable that is present within the current instance of the shell. It is not available to
programs that are started by the shell.
2. Environment Variables
An environment variable is a variable that is available to any child process of the shell. Some programs need
environment variables in order to function correctly. Usually a shell script defines only those environment variables that
are needed by the programs that it runs.
3. Shell Variables
A shell variable is a special variable that is set by the shell and is required by the shell in order to function
correctly. Some of these variables are environment variables whereas others are local variables.
export modifies or prints the environment.
You place variables in the environment by exporting them. Exporting can be done as follows:
export name=valueIn this form, the variable specified by name is assigned the given value. Then that variable is marked for export. The environment is simply a list of name-value pairs that is available toevery running program. New processes inherit the environment from their parent,and are able to modify it before creating new child processes of their own. 局部变量是普通的变量,仅在创建它的Shell中有效。 环境变量对创建它的Shell及其派生出来的子进程都有效。或者说环境变量可以继承,但它对其它与创建它的Shell没有关系的Shell并不可见。其实重启一个终端就是重新启动了一个shell .Linux的环境变量具有继承性,即子shell会继承父shell的环境变量。export 用于把变量变成当前shell和其子shell的环境变量,存活期是当前的shell及其子shell,因此重新登陆以后,它所设定的环境变量就消失了。 source 当直接执行一个脚本的时候,其实是在一个子shell环境运行的,即开启了一个子shell来执行这个脚本,脚本执行完后该子shell自动退出。环境变量名可以用到程序(进程)里,程序(进程)可以获得它的值,根据值决定如何操作,运行,找路径,文件夹等等。
Environment variableEnvironment variables are a set of dynamic named values that can affect the way running processes will behave on a computer.
They can be said in some sense to create the operating environment in which a process runs.
Running programs can access the values of environment variables for configuration purposes.
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 the script, although in Unix other variables are usually used for this.
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.