删除 Bash 中的位置参数? [英] Delete positional parameters in Bash?

查看:19
本文介绍了删除 Bash 中的位置参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您可以使用 shift 跳过位置参数,但是您可以通过传递位置来删除位置参数吗?

You can skip positional parameters with shift but can you delete positional parameters by passing the position?

x(){ CODE; echo "$@"; }; x 1 2 3 4 5 6 7 8
> 1 2 4 5 6 7 8

我想在 x() 中添加 CODE 来删除位置参数 3.我不想做 echo "${@:1:2} ${@:4:8}".运行 CODE 后,$@ 应该只包含1 2 4 5 6 7 8".

I would like to add CODE to x() to delete positional parameter 3. I don't want to do echo "${@:1:2} ${@:4:8}". After running CODE, $@ should only contain "1 2 4 5 6 7 8".

推荐答案

最好的办法,如果你希望能够将参数传递给另一个进程,或者处理空格分隔的参数,就是重新set 参数:

The best way, if you want to be able to pass on the parameters to another process, or handle space separated parameters, is to re-set the parameters:

$ x(){ echo "Parameter count before: $#"; set -- "${@:1:2}" "${@:4:8}"; echo "$@"; echo "Parameter count after: $#"; }
$ x 1 2 3 4 5 6 7 8
Parameter count before: 8
1 2 4 5 6 7 8
Parameter count after: 7

要测试它是否适用于非平凡参数:

To test that it works with non-trivial parameters:

$ x $'a
1' $'b2' 'c 3' 'd 4' 'e 5' 'f 6' 'g 7' $'h	8'
Parameter count before: 8
a
1 2 d 4 e 5 f 6 g 7 h   8
Parameter count after: 7

(是的,$'' 是退格)

这篇关于删除 Bash 中的位置参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆