获取Bash数组中的最后一个元素 [英] Get last element in Bash array

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

问题描述

说我有一个数组:

arr=(a b c d e f)

如果要获取数组的最后一个元素,通常必须获取元素的总数,减去一个,然后使用该数字作为索引:

If I want to get the last element of the array, I normally have to get the total number of elements, substract one and use that number to call as an index:

$ echo ${#arr[@]}
6
$ echo ${arr[${#arr[@]}-1]}
f

但是,我看到了最近(Bash 4.2-4.3)可以使用负索引:

However, I see that recently (Bash 4.2 - 4.3) you can use negative indexes:

$ echo ${arr[-1]}
f
$ echo ${arr[-2]}
e

所以我想知道:什么时候引入的?

So I am wondering: when was this introduced? Is it also usable by other shells like ksh, zsh...?

我的研究表明:

Bash-4.3-rc1可用用于FTP

a.修复了一个错误,该错误导致使用 下标为负会导致分割错误.

a. Fixed a bug that caused assignment to an unset variable using a negative subscript to result in a segmentation fault.

b.修复了导致使用以下命令分配给字符串变量的错误: 负下标以使用错误的索引.

b. Fixed a bug that caused assignment to a string variable using a negative subscript to use the incorrect index.

...

x. Shell现在允许分配,引用和取消设置元素 带有负下标(a [-1] = 2,echo $ {a [-1]}的索引数组) 从数组的最后一个元素开始倒数.

x. The shell now allows assigning, referencing, and unsetting elements of indexed arrays using negative subscripts (a[-1]=2, echo ${a[-1]}) which count back from the last element of the array.

关于阵列的Bash手册4.3

引用没有下标的数组变量等效于 下标为0引用.如果下标用于引用 索引数组的元素的计算结果为小于零的数字, 它被解释为相对于大于最大索引的1 数组,因此负索引从数组末尾算起, 并且索引-1表示最后一个元素.

Referencing an array variable without a subscript is equivalent to referencing with a subscript of 0. If the subscript used to reference an element of an indexed array evaluates to a number less than zero, it is interpreted as relative to one greater than the maximum index of the array, so negative indices count back from the end of the array, and an index of -1 refers to the last element.

但是我想知道这是否已经存在于Bash 4.2中,因为第一个资源提到了已修复的错误.

But I wonder if this was already in Bash 4.2, since the first resource mentions a bug that was fixed.

推荐答案

据我在

As far as I can see in https://tiswww.case.edu/php/chet/bash/CHANGES, the new feature is in this part :

该文档详细介绍了此版本 bash-4.3-alpha, 以及以前的版本bash-4.2-release .

This document details the changes between this version, bash-4.3-alpha, and the previous version, bash-4.2-release.

...

x.现在,shell允许使用负下标(a [-1] = 2,echo $ {a [-1]})分配,引用和取消设置索引数组的元素,这些元素从数组的最后一个元素开始计数.

x. The shell now allows assigning, referencing, and unsetting elements of indexed arrays using negative subscripts (a[-1]=2, echo ${a[-1]}) which count back from the last element of the array.

修复:

本文档详细介绍了此版本 bash-4.3-beta2和先前版本bash-4.3-beta 之间的更改.

1对Bash的更改

a.修复了导致 使用负下标分配给未设置的变量会导致分段错误.

a. Fixed a bug that caused assignment to an unset variable using a negative subscript to result in a segmentation fault.

b.修复了导致使用负下标分配给字符串变量以使用错误索引的错误.

b. Fixed a bug that caused assignment to a string variable using a negative subscript to use the incorrect index.

它修复了Bash 4.3中的一项新功能.

It a fix of a new feature in Bash 4.3.

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

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