语法:
function fname(){
程序段
}
ps:函数在执行时,必须已经被定义过,即函数定义最好写在最前面。
pps:有的shell函数定义没有(),即function fname{}
调用:
fname args... //没有()与形参列表,参数写在函数名后面!!
内建变量:
function也有内建变量,与shell script类似,函数名称表示为$0,参数用$1,$2,$3....表示。
ps:函数中的$0与shell script中的$0是不同的,要注意区别!
#!/bin/bash -e
function printit(){
echo “Your choice is $1”
}
case $1 in
“one”)
printit 1;;
“two”)
printit 2;;
esac
#bash sh01.sh one
结果Your choice is 1