"export" in SHELL

  在shell中,若某一变量需要在随后的子程序中运行,则需要以 export 来使变量变成环境变量:  export var

export的用法总结:

1:一个shell中用export定义的变量,只对当前shell以及它的子shell有效,不会对其父shell有效。[下面的这个示例有点儿乱,如果你理解这句话的话可以不看]

lxw 23:51:20:~$ echo $var    # A shell

lxw 23:51:57:~$ bash         # B shell
lxw 23:53:09:~$ echo $var

lxw 23:53:12:~$ var="Hello World"
lxw 23:53:20:~$ echo $var
Hello World
lxw 23:53:23:~$ export var
lxw 23:53:27:~$ echo $var    #当前shell(B shell)中有效
Hello World
lxw 23:53:31:~$ bash         # C shell
lxw 23:53:38:~$ echo $var    #子shell(C shell)中有效
Hello World
lxw 23:53:43:~$ exit
exit
lxw 23:53:55:~$ echo $var    # B shell
Hello World
lxw 23:53:58:~$ exit
exit
lxw 23:54:12:~$ echo $var    #父shell(A shell)中无效

lxw 23:54:18:~$ 

2:不用export定义的变量只对该shell有效,对子shell也是无效的。

lxw 23:47:59:~$ echo $var

lxw 23:48:11:~$ var="Hello World"
lxw 23:48:28:~$ echo $var
Hello World
lxw 23:48:30:~$ bash         
lxw 23:49:18:~$ echo $var    #未使用export,子shell中无效

lxw 23:49:21:~$ exit
exit
lxw 23:49:24:~$ echo $var
Hello World
lxw 23:49:27:~$ 
原文地址:https://www.cnblogs.com/lxw0109/p/export_in_shell.html