shell子進程修改父進程的環(huán)境變量值
腳本中的環(huán)境變量通過 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