Linux|shell——全局变量与局部变量

变量的作用域 一、全局变量: 任何地方都生效的变量,默认情况下,脚本主体内定义全局变量,函数内可以用,函数外也可以用

[root@server ~]# vim overall.sh #!/bin/bashfunction fun1() { temp=$[ $value + 5 ] result=$[ $temp * 2 ] }temp=4 value=https://www.it610.com/article/6fun1echo"The result is $result" if [ $temp -gt $value ]; then echo "temp is larger" else echo "temp is smaller" fi [root@server ~]# sh overall.sh The result is 22 temp is larger

【Linux|shell——全局变量与局部变量】二、局部变量:
  • 定义方法: local value
[root@server ~]# vim local.sh #!/bin/bashfunction fun1() { local temp=$[ $value + 5 ] result=$[ $temp * 2 ] }temp=4 value=https://www.it610.com/article/6fun1echo"The result is $result" if [ $temp -gt $value ]; then echo "temp is larger" else echo "temp is smaller" fi [root@server ~]# sh local.sh The result is 22 temp is smaller

    推荐阅读