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

相關文章
相關標籤/搜索