Bash函数:获取传递参数的数量
时间:2020-01-09 10:45:53 来源:igfitidea点击:
我如何确定在类似操作系统的Unix下传递给bash函数foo()的参数的数量?每个bash shell函数都有以下shell变量集:
所有函数参数或者参数都可以通过
$1,$2,$3,...,$N访问。$*或者$@保存所有传递给函数的参数或者参数。
$#保留传递给函数的位置参数的数量。
名为FUNCNAME的数组变量保留当前在执行调用堆栈中的所有shell函数的名称。
例子
创建一个shell脚本,如下所示:
#!/bin/bash
# Purpose: Demo bash function
# Author: theitroad
# ----------------------------
## Define a function called foo()
foo(){
echo "Function name: ${FUNCNAME}"
echo "The number of positional parameter : $#"
echo "All parameters or arguments passed to the function: '$@'"
echo
}
## Call or invoke the function ##
## Pass the parameters or arguments ##
foo theitroad
foo 1 2 3 4 5
foo "this" "is" "a" "test"
如下运行:
$ chmod +x script.name.here $ ./script.name.here

