简体   繁体   English

获取除第一个之外的 bash 数组的所有元素

[英]Getting all elements of a bash array except the first

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].我有一个索引 bash 数组,我想使用像“${a[@]}”这样的表达式,但我希望它不包含 a[0]。 The best that I can think of is this:我能想到的最好的是:

j=0
for i in "${a[@]}"
do
    b[j]=${a[++j]}
done

and then use "${b[@]}".然后使用“${b[@]}”。 Is there a better way?有没有更好的办法?

$ a=(1 2 3)
$ echo "${a[@]:1}"
2 3

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 是脚本的名称)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM