linux中export与source详解

在linux找那个我们有时会需要设置一些环境变量来方便我们的工作,永久生效,暂时生效,全局生效,当前环境有效,激活隔离环境等,这就用到了export,source

父shell与子shell

首先说明两个概念:
父shell与子shell,从shellA中启动一个shell,称之为shellB。shellA为父shell,shellB为子shell。

最容易理解的情况就是在一个shell中执行一个gnome-terminal命令(不同桌面环境命令不一样),弹出一个新的shell

最常见的情况是在当前shell下执行shell脚本,这个脚本实际上是在子shell中执行的

环境变量与局部变量

环境变量在全局都有效,而局部变量只在当前shell中有效

linux中export命令:

将局部变量申明为环境变量

用法如:export JAVA_HOME=/opt/jdk

Linux source命令:

用法:source filepath. filepath

功能:使当前shell读入路径为filepath的shell文件并依次执行文件中的所有语句,通常用于重新执行刚修改的初始化文件,使之立即生效,而不必注销并重新登录。例如,当我们修改了/etc/profile文件,并想让它立刻生效,而不用重新登录,就可以使用source命令,如source /etc/profile

source命令(从 C Shell 而来)是bash shell的内置命令;点命令(从Bourne Shell而来)是source的另一名称。这从用法中也能看出来。

source filepathsh filepath./filepath的区别:

  • 当shell脚本具有可执行权限时,用sh filepathsource filepath,./filepath是没有区别的。

[root@localhost scripts]# chmod +x test.sh
[root@localhost scripts]# . test.sh
hello,world
[root@localhost scripts]# source test.sh 
hello,world
[root@localhost scripts]# sh test.sh 
hello,world
[root@localhost scripts]# . ./test.sh
hello,world
[root@localhost scripts]# source ./test.sh 
hello,world
[root@localhost scripts]# sh ./test.sh 
hello,world
[root@localhost scripts]# ./test.sh 
hello,world
[root@localhost scripts]#

注意.命令与路径中的.(代表当前路径)不一样

  • sh filepath 会重新建立一个子shell,在子shell中执行脚本里面的语句,该子shell继承父shell的环境变量,但子shell新建的,改变的变量不会被带回父shell,除非使用export。

  • source filename其实只是简单地读取脚本里面的语句依次在当前shell里面执行,没有建立新的子shell。那么脚本里面所有新建、改变变量的语句都会保存在当前shell中。

source用法举例说明:


[root@localhost scripts]# vim demo.sh 
[root@localhost scripts]# cat demo.sh 
A=1
[root@localhost scripts]# chmod +x demo.sh 
[root@localhost scripts]# ll
total 16
-rwxr-xr-x. 1 root root  4 Aug  1 14:42 demo.sh
-rw-r--r--. 1 root root 18 Aug  1 14:07 exp1.sh
-rw-r--r--. 1 root root 29 Aug  1 14:08 exp2.sh
-rw-r--r--. 1 root root 10 Aug  1 14:08 test.sh
[root@localhost scripts]# sh demo.sh 
[root@localhost scripts]# echo $A

[root@localhost scripts]# ./demo.sh 
[root@localhost scripts]# echo $A

[root@localhost scripts]# . demo.sh 
[root@localhost scripts]# echo $A
1
[root@localhost scripts]# unset A  # unset的作用为删除指定变量
[root@localhost scripts]# echo $A

[root@localhost scripts]# source demo.sh 
[root@localhost scripts]# echo $A
1
[root@localhost scripts]#

实验验证

这里用最常见的情况举例:在当前shell下执行脚本

现在有三个脚本

exp1.sh内容为


var="hello,world"

exp2.sh内容为


var="hello,world"
export var

test.sh内容为


echo $var

实验1:


[root@localhost scripts]# ll
total 12
-rw-r--r--. 1 root root 18 Aug  1 14:07 exp1.sh
-rw-r--r--. 1 root root 29 Aug  1 14:08 exp2.sh
-rw-r--r--. 1 root root 10 Aug  1 14:08 test.sh
[root@localhost scripts]# source exp1.sh
[root@localhost scripts]# echo $var
hello,world
[root@localhost scripts]# bash test.sh

[root@localhost scripts]#

实验2:


[root@localhost scripts]# ll
total 12
-rw-r--r--. 1 root root 18 Aug  1 14:07 exp1.sh
-rw-r--r--. 1 root root 29 Aug  1 14:08 exp2.sh
-rw-r--r--. 1 root root 10 Aug  1 14:08 test.sh
[root@localhost scripts]# source exp2.sh 
[root@localhost scripts]# echo $var
hello,world
[root@localhost scripts]# bash test.sh 
hello,world
[root@localhost scripts]#

可以看到执行exp脚本之后,在当前shell下执行echo $var是可以输出var的值的,因为source会读取exp脚本的内容并执行文件中的语句,使var在当前shell中有值。

执行bash test.sh时,实验1是没有任何输出,因为在执行test脚本产生的子shell中找不到var这个变量;而实验2 echo出了var的值,因为实验2中exp2.sh加入了export,所以var变成了环境变量(全局有效),所以var对子shell是可见的,而实验1中由于没有export var,所以var是个局部变量,并不能被子shell看到。

同样的,linux中在 profilebashrc或者其他类似的文件中设置环境变量时(比如PATH),如果没有export,那么只能在当前shell中起作用;如果在当前shell下运行脚本或者直接启动一个子shell,因为设置变量的是局部变量,那么在子shell中该变量没有值。


参考:https://blog.csdn.net/damontive/article/details/50352722

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值