有关shell function参数捕捉

发布时间 2023-03-22 21:16:01作者: azureology

需求

使用shell script希望将上层函数的参数转发给内层函数处理。

实现

容易想到使用$@对参数进行通配

show()
{
    echo "------title ${1}---------"
    echo ${@}
    echo "------ending-------------"
}
show 111 222

输出结果为

------title 111---------
111 222
------ending-------------

问题

显然作为$1111被重复捕捉了两次,需要在$@中进行剔除
可以使用${@:2}${*:2}
区别在于参数中存在空格时${@:2}会转发多个而${*:2}视为一个

show()
{
    echo "------title ${1}---------"
    echo ${@:2}
    echo "------ending-------------"
}
show 111 222

输出结果为

------title 111---------
222
------ending-------------

参考

shell - Process all arguments except the first one (in a bash script) - Stack Overflow