一、全局变量: 任何地方都生效的变量,默认情况下,脚本主体内定义全局变量,函数内可以用,函数外也可以用
[root@server ~]# vim overall.sh #!/bin/bash function fun1() { temp=$[ $value + 5 ] result=$[ $temp * 2 ] } temp=4 value=6 fun1 echo "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二、局部变量:
定义方法: local value [root@server ~]# vim local.sh #!/bin/bash function fun1() { local temp=$[ $value + 5 ] result=$[ $temp * 2 ] } temp=4 value=6 fun1 echo "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