#!/bin/sh
# childDemo.sh
echo "`basename ${0}` the variables is: "
echo "A: $A"
echo "Welcome: $Welcome"
echo "Author: $Author"
echo "Version $Version"
#!/bin/sh
# parentDemo.sh
export Welcome="Hello variables"
Author="Lesus"
Version=0.1
localF(){
local A=10
Version=0.2
echo "A: $A"
echo "Welcome:$Welcome"
echo "Author: $Author"
echo "Version: $Version"
}
echo "call localF function"
localF
echo "========================"
echo "Now the variables is: "
echo "A: $A"
echo "Welcome: $Welcome"
echo "Author: $Author"
echo "Version $Version"
echo "========================"
echo "Using default"
./childDemo.sh
echo "========================"
echo "Using source"
. ./childDemo.sh
echo "======================="
echo "Using exec"
exec ./childDemo.sh
好了,相关材料都齐全了,
关于默认的执行脚本方式,source和exec方式我会在下一篇里介绍。最好在理解了这三者的关系,对作用域才会有更好的理解。
现在总结一下:
作用域关键字有三个: 范围从大到小依次是
export 作用域范围为本身shell和它的子shell中
默认 作用域范围为本身shell
local 作用域范围为本身shell中特定的范围