在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:~$ 

 

相关文章:

  • 2021-12-14
  • 2021-11-30
  • 2021-11-23
  • 2022-12-23
  • 2022-01-26
  • 2021-08-27
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-10-17
  • 2021-09-29
  • 2021-12-24
  • 2021-06-19
  • 2021-11-30
相关资源
相似解决方案