function NAME { COMMANDS ; }
# 或
NAME { COMMANDS ; }
!subtitle:功能
定义函数。
调用函数时可以通过 $1,%2,... 依次读取实参;变量 FUNCNAME 的值会被设为函数名。
!subtitle:类型
Bash 内置命令。
!subtitle:参数
NAME - 函数名
VALUE - 函数中执行的命令
$ function greet() # 定义函数
> {
> echo $FUNCNAME: Hello $1
> }
$ greet # 调用函数
greet: Hello
$ greet Jerry # 带参数调用函数
greet: Hello Jerry
function: function name { COMMANDS ; } or name () { COMMANDS ; }
Define shell function.
Create a shell function named NAME. When invoked as a simple command,
NAME runs COMMANDs in the calling shell's context. When NAME is invoked,
the arguments are passed to the function as $1...$n, and the function's
name is in $FUNCNAME.
Exit Status:
Returns success unless NAME is readonly.