函數(shù)定義
函數(shù)的定義格式:
[ function ] funname [()]
{
action;
[return int;]
}
可以帶 function fun() 定義碰凶,也可以直接 fun() 定義,不帶任何參數(shù)烈涮。
參數(shù)返回烈疚,可以顯示加:return 返回,如果不加留攒,將以最后一條命令運行結(jié)果煤惩,作為返回值。 return 后跟數(shù)值 n(0-255)
函數(shù)的參數(shù)
調(diào)用函數(shù)時可以向其傳遞參數(shù)稼跳。在函數(shù)體內(nèi)部盟庞,通過 $n 的形式來獲取參數(shù)的值,例如汤善,
$1` 表示第一個參數(shù)什猖,$2 表示第二個參數(shù)...
(注意:$10 不能獲取第十個參數(shù)票彪,獲取第十個參數(shù)需要 ${10}。當 n>=10 時不狮,需要使用 ${n} 來獲取參數(shù)降铸。)
funWithParam(){
echo "The first parameter is $1 !"
echo "The second parameter is $2 !"
echo "The tenth parameter is $10 !"
echo "The tenth parameter is ${10} !"
echo "The eleventh parameter is ${11} !"
echo "The total number of parameters is $# !"
echo "Outputs all parameters as a string $* !"
}
funWithParam 1 2 3 4 5 6 7 8 9 34 73