Shell 函式的使用
阿新 • • 發佈:2018-11-11
Shell 函式
shell函式定義
- 1、可以帶function fun() 定義,也可以直接fun() 定義,不帶任何引數。
- 2、引數返回,可以顯示加:return 返回,如果不加,將以最後一條命令執行結果,作為返回值。 return後跟數值n(0-255)
函式引數
在Shell中,呼叫函式時可以向其傳遞引數。在函式體內部,通過 $n 的形式來獲取引數的值,例如,$1表示第一個引數,$2表示第二個引數... 注意,$10 不能獲取第十個引數,獲取第十個引數需要${10}。當n>=10時,需要使用${n}來獲取引數。
函式示例:
$ funWithParam() { echo "The first para $1 !" echo "The second para $2 !" echo "The tenth para $10 !" echo "The tenth para ${10} !" echo "The total para $# !" echo "output as a string $* !" echo `date` } #執行結果如下 $ funWithParam 1 2 3 4 5 6 7 8 9 55 66 77 The first para 1 ! The second para 2 ! The tenth para 10 ! The tenth para 55 ! The total para 12 ! output as a string 1 2 3 4 5 6 7 8 9 55 66 77 ! 2018年10月10日 14:00:48