在linux的环境变量中,通过在父进程中使用“export 变量名”,可以将变量导入到子进程中。还可以通过在脚本中调用另一个脚本(实际上创建了一个子进程)。
在father脚本中设置变量名为“FILM”,取值为“A FEW GOOD Men”,再在father脚本中调用child脚本,child脚本中先打印“FILM”的值,后在修改“FILM”的值为“Die Hard”,例程如下:
father的脚本:
#!/bin/sh
#father script.
echo "this is the father!"
FILM="A FEW GOOD Men"
FILM //没有导入到子进程中
echo "the :$FILM"
./child
echo "back to father"
echo "and the FILM:$FILM"
child的脚本:
#!/bin/sh
echo "call from father, i child"
echo "film :$FILM"
FILM="Die Hard"
echo "changing film to :$FILM"
echo "exit child"
ubuntu环境中运行的结果:
在父进程中将变量名导入到子进程中,添加export:
#!/bin/sh
#father script.
echo "this is the father!"
FILM="A FEW GOOD Men"
export FILM
echo "the :$FILM"
./child
echo "back to father"
echo "and the FILM:$FILM"