6.set命令
在$HOME.profile文件中,设置环境变量时,除了export可以导出之外,还有set -a命令,
可以指明所有变量直接被导出;不过别在/etc/profile中设置,在自己的$HOME.profile中设置。
下面是一个shell脚本的实例
在father.sh脚本中,调用child.sh脚本,并修改father.sh中的变量值,如下:
pg father.sh
# father script
echo "This is the father"
SMART_PHONE="iPhone 5"
echo "I have the phone:$SMART_PHONE"
# call the child script
sh child.sh
echo "The phone father owned is:$SMART_PHONE"
pg child.sh
# child
echo "called from father..i am the child"
echo "phone is:${SMART_PHONE:-unknow}"
SMART_PHONE="Meizu MX2"
echo "changing SMART_PHONE to:$SMART_PHONE"
执行脚本:
sh father.sh
This is the father
I have the phone:iPhone 5
called from father..i am the child
phone is:unknow ->①不认识父进程中的变量
changing SMART_PHONE to:Meizu MX2
The phone father owned is:iPhone 5
可以看到,在子进程中并不认识父进程中的变量,这是因为我们没在父进程中导出变量值。
修改father.sh脚本:
pg father.sh
# father script
echo "This is the father"
SMART_PHONE="iPhone 5"
echo "I have the phone:$SMART_PHONE"
# call the child script
export SMART_PHONE ->②
sh child.sh
echo "The phone father owned is:$SMART_PHONE"
现在执行修改后的脚本,子进程认识父进程传递过来的变量了:
sh father.sh
This is the father
I have the phone:iPhone 5
called from father..i am the child
phone is:iPhone 5 ->①导出之后,认识了
changing SMART_PHONE to:Meizu MX2
The phone father owned is:iPhone 5
