shell子进程修改父进程的环境变量值
脚本中的环境变量通过 export 导出,脚本中调用其他脚本使用这个变量
这里有两个脚本程序 hello 和 hello1
hello 脚本代码
#!/bin/bash
FILM="Front of the class"
#export FILM 这里我注释掉 export 命令
echo $FILM
./hello1 ##调用./hello1脚本,打印FILM,注意这里是 父与子 进程的调用关系
hello1 脚本程序
#!/bin/bash
echo "$FILM in hello1" 打印FILM变量
如果我们注释掉export 输出变量,那么在 hello1 只是打印出 in hello1 ,引文FILM 是空
打印输出
#:~/yu/course/hello$ ./hello
Front of the class
in hello1
注意一点:./hello1 父子进程调用关系,hello1
是在 hello
开辟的子进程中运行
如果在子进程中修改 FILM 的值,会不会在 父进程中改变呢??
不会,首先,通过./hello1 方式调用,是父子进程的关系,export 是单向传递,从父进程到子进程,不能从子进程到父进程。当子进程撤消后,变量值也就消失了,不会改变变量值
打印子进程中修改过的变量值,使用 "." 点命令. ./hello1
这个方式调用
这种方式就可以使得 hello
和 hello1
在同一个进程中了,变量可以传值了在 hello 中修改为
#!/bin/bash
FILM="Front of the class"
export FILM
echo $FILM before hello
. ./hello1
echo $FILM after hello
#:~/yu/course/hello$ ./hello
Front of the class before hello
Front of the class in hello1 first
MODIFY in hello1 second
MODIFY after hello
#:~/yu/
songshu.png
网友评论