日期:2014-05-16  浏览次数:20580 次

leeboy的linux学习十三环境变量实例

环境变量和本地变量的区别在于是不是具有继承性,下面介绍一个环境变量的例子:

exportFather父进程:

#!/bin/sh
#father script
echo "this is the father"
FILE="A GOOD MAN"
echo "I like the file : $FILE"
export FILE     #声明为环境变量,如果此处不声明,exportChild无法使用该变量
./exportChild   #执行子进程
echo "back to father"
echo "and the file is : $FILE"


exportChild  子进程:

#!/bin/sh
#child file
echo "this is Child"
echo "I like my father's file : $FILE"
FILE="Child File"           #子进程中修改该环境变量对主进程中没有影响
echo "my file is : $FILE"


执行结果如下:

[root@localhost textFile]# ./exportFather 
this is the father
I like the file : A GOOD MAN
this is Child
I like my father's file : A GOOD MAN
my file is : Child File
back to father
and the file is : A GOOD MAN		//环境变量在子进程中修改但父进程中没有变化


 如果我想在子进程中修改的环境变量也能影响到主进程的改环境变量,该如何做呢?望大牛指导!