重击:除最后一个元素外的所有数组 [英] Bash: all of array except last element

查看:78
本文介绍了重击:除最后一个元素外的所有数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Bash具有一种巧妙的方式来赋予数组中除第一个元素之外的所有元素:

Bash has a neat way of giving all elements in an array except the first:

"${a[@]:1}"           

获取除我找到的最后一个以外的所有内容:

To get all except the last I have found:

"${a[@]:0:$((${#a[@]}-1))}"

但是,老兄,这很丑.

有一个优雅的选择吗?

推荐答案

我不确定会有什么改进,但是您可以删除算术运算符($(()))和起始索引(此处为0):

I am not sure how much improvement it would be, but you can drop the arithmetic operator ($(())) and starting index (0 here):

${a[@]::${#a[@]}-1}

所以:

$ foo=( 1 2 3 )

$ echo "${foo[@]::${#foo[@]}-1}"
1 2

如您所见,这种改进纯粹是句法上的;这个想法保持不变.

As you can see, the improvement is purely syntactical; the idea remains the same.

这篇关于重击:除最后一个元素外的所有数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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