Bash arrays and negative subscripts, yes or no?

浪尽此生 提交于 2019-12-03 05:51:42

If you just want the last element

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

If you want next to last element

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

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.

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

Fails for me in CentOS 6.3 (bash 4.1.2)

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:

line 46: [-1]: bad array subscript

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!