Bash 数组和负下标,是还是不是?

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

Bash arrays and negative subscripts, yes or no?

linuxarraysbashunixgnu

提问by bob.sacamento

The GNU bash manualtells me

GNU bash的手册告诉我

An indexed array is created automatically if any variable is assigned to using the syntax

name[subscript]=value

The subscript is treated as an arithmetic expression that must evaluate to a number. If subscript evaluates to a number less than zero, it is used as an offset from one greater than the array's maximum index (so a subcript of -1 refers to the last element of the array).

如果使用以下语法将任何变量分配给了一个索引数组,则会自动创建一个索引数组

name[subscript]=value

下标被视为必须计算为数字的算术表达式。如果下标计算结果为小于零的数字,则将其用作与大于数组最大索引的数字的偏移量(因此 -1 的下标指的是数组的最后一个元素)。

So I figure I will give it a try and get the following result:

所以我想我会尝试一下并得到以下结果:

$ muh=(1 4 'a' 'bleh' 2)
$ echo $muh
1
$ echo ${muh[*]}
1 4 a bleh 2    # so far so good so now I'll try a negative ...
$ echo ${muh[-1]}
-bash: muh: bad array subscript  # didn't go as planned!

Did I do something wrong, or is the website wrong, or is gnu bash that different from the bash I am running under CentOS? Thanks!

是我做错了什么,还是网站有问题,或者 gnu bash 与我在 CentOS 下运行的 bash 不同?谢谢!

回答by Steven Penny

If you just want the last element

如果你只想要最后一个元素

$ echo ${muh[*]: -1}
2

If you want next to last element

如果你想在最后一个元素旁边

$ echo ${muh[*]: -2:1}
bleh

回答by kojiro

According to Greg Wooledge's wiki, (which links to the bash changelog) the negative index syntax was added to bash in version 4.2 alpha.

根据Greg Wooledge 的 wiki(链接到 bash 更改日志),负索引语法在 4.2 alpha 版中添加到 bash。

回答by stark

If you do man bashthe section on arrays does not list this behavior. It might be something new (gnu?) in bash.

如果您执行man bash有关数组的部分,则不会列出此行为。它可能是 bash 中的新东西(gnu?)。

Fails for me in CentOS 6.3 (bash 4.1.2)

在 CentOS 6.3 (bash 4.1.2) 中我失败了

回答by Ludi

The negative subscript works perfectly fine for me on my computer with Ubuntu 14.04 / GNU bash version 4.3.11(1) however it returns:

负下标在我的 Ubuntu 14.04 / GNU bash 版本 4.3.11(1) 计算机上对我来说非常好,但是它返回:

line 46: [-1]: bad array subscript

When I try to run the same script on 4.2.46(1). I

当我尝试在 4.2.46(1) 上运行相同的脚本时。一世

回答by tlwhitec

Old bashes (like the default one on Macs these days) don't support negative subscripts. Apart from the "substring expansion" used in the accepted answer, a possible workaround is to count the desired index from the array start within the brackets:

旧的 bash(就像现在 Mac 上的默认 bash)不支持负下标。除了在接受的答案中使用的“子字符串扩展”之外,一种可能的解决方法是从括号内的数组开始计算所需的索引:

$ array=(one two three)
$ echo "${array[${#array[@]}-1]}"
three

With this approach, you can pack other parameter expansion operations into the term, e.g. "remove matching prefix pattern" th:

使用这种方法,您可以将其他参数扩展操作打包到术语中,例如“删除匹配的前缀模式” th

$ echo "${array[${#array[@]}-1]#th}"
ree