获取除第一个之外的 bash 数组的所有元素
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6287419/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Getting all elements of a bash array except the first
提问by grok12
I have an indexed bash array and I'd like to use an expression like "${a[@]}" except I want it to not include a[0]. The best that I can think of is this:
我有一个带索引的 bash 数组,我想使用像“${a[@]}”这样的表达式,但我希望它不包含 a[0]。我能想到的最好的是:
j=0
for i in "${a[@]}"
do
b[j]=${a[++j]}
done
and then use "${b[@]}". Is there a better way?
然后使用“${b[@]}”。有没有更好的办法?
回答by Ignacio Vazquez-Abrams
$ a=(1 2 3)
$ echo "${a[@]:1}"
2 3
回答by Tom Hale
If it's a standard array, use:
如果是标准数组,请使用:
"${a[@]:1}"
If you're working with parameters:
如果您正在使用参数:
"${@:2}"
Note the different syntax and that $@is 1-indexed (since $0 is the name of the script).
请注意不同的语法,即$@1 索引(因为 $0 是脚本的名称)。

