046-变量的做用域

1. 在脚本中在继续执行脚本

[root@cnsz142728 September]# ./Namespace01.sh 
xxaa ./Namespace01.sh:100
xxaa in sss.sh:200
[root@cnsz142728 September]# cat Namespace01.sh 
#!/bin/bash
xxaa=100
echo xxaa $0:$xxaa
bash sss.sh
[root@cnsz142728 September]# cat sss.sh 
#!/bin/bash
xxaa=200
echo xxaa in $0:$xxaa

2.在脚本中调用函数

[root@cnsz142728 September]# vim ccc.sh
#/!/bin/bash
xxaa=1000
function xx(){
xxaa=2000
}
echo "Before function xxaa:$xxaa"

xx
echo "after function xxaa:$xxaa"

"ccc.sh" [New] 9L, 121C written                               
[root@cnsz142728 September]# chmod +x ccc.sh  
[root@cnsz142728 September]# ./ccc.sh 
Before function xxaa:1000
after function xxaa:2000

#############
若是加上local 则:
function xx(){
local xxaa=2000
}

[root@cnsz142728 September]# ./ccc.sh 
Before function xxaa:1000
after function xxaa:1000

local的出现,会使局部变量受到影响,全局变量不会受到影响vim

相关文章
相关标签/搜索