1、定义格式:function functionName() #关键字function可以省略{ list of commands [return value]}函数返回值: #函数的返回值只能是整数 可以显式增加return语句;如果不加,会将最后一条命令运行结果作为返回值。 eg: #!/bin/bashfunWithReturn(){echo -n"Input first number: "read aNumecho -n"Input another number: "read anotherNumecho "The two numbers are $aNum and $anotherNum !"return$(($aNum+$anotherNum))}funWithReturn #调用函数funWithReturnret=$? #读取返回值echo "The sum of two numbers is $ret !"2、带参函数 特殊变量:
$#传递给函数的参数个数。$*显示所有传递给函数的参数。$@与$*相同,但是略有区别$?函数的返回值。
eg: #!/bin/bashfunWithParam(){echo "The value of the first parameter is $1 !"echo "The value of the second parameter is $2 !"echo "The value of the tenth parameter is $10 !"echo "The value of the tenth parameter is ${10} !"echo "The value of the eleventh parameter is ${11} !"echo "The amount of the parameters is $# !"# 参数个数echo "The string of the parameters is $* !"# 传递给函数的所有参数}funWithParam 1234567893473 #参数之间以空格区分3、删除函数$unset .f functionName 4、文件包含 在一个脚本文件中调用另一个脚本文件(被调用的shell脚本可以没有执行权限) .空格fileName eg: . /home/text.sh 或 source空格fileName eg: source /home/text.sh 这样就可以调用另一个脚本中的函数或者变量了