获取 Bash 数组中的最后一个元素

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36977855/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 14:35:36  来源:igfitidea点击:

Get last element in Bash array

arraysbashshell

提问by fedorqui 'SO stop harming'

Say I have an array:

假设我有一个数组:

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

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

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

$ 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...?

所以我想知道:这是什么时候引入的?它是否也可以被 ksh、zsh 等其他 shell 使用?

My research shows:

我的研究表明:

Bash-4.3-rc1 available for FTP

Bash-4.3-rc1 可用于 FTP

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

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

...

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.

一种。修复了导致使用负下标分配给未设置变量导致分段错误的错误。

湾 修复了导致使用负下标为字符串变量赋值以使用不正确索引的错误。

...

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

And Bash manual 4.3, on Arrays

Bash 手册 4.3,关于数组

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.

引用一个不带下标的数组变量等同于用下标 0 引用。如果用于引用索引数组元素的下标计算结果小于零,则将其解释为相对于大于最大索引的 1数组,所以负索引从数组的末尾开始计数,索引 -1 指的是最后一个元素

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

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

采纳答案by SLePort

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

据我在https://tiswww.case.edu/php/chet/bash/CHANGES 中看到,新功能在这一部分:

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

...

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-alpha 与先前版本 bash-4.2-release之间的更改。

...

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

The fix in :

修复:

This document details the changes between this version, bash-4.3-beta2, and theprevious version, bash-4.3-beta.

1 Changes to Bash

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

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

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

1 对 Bash 的更改

一种。修复了导致使用负下标分配给未设置变量导致分段错误的错误。

湾 修复了导致使用负下标为字符串变量赋值以使用不正确索引的错误。

It a fix of a new feature in Bash 4.3.

它修复了 Bash 4.3 中的一个新功能。