Get last element in Bash array

后端 未结 1 1262
没有蜡笔的小新
没有蜡笔的小新 2020-12-31 10:35

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,

相关标签:
1条回答
  • 2020-12-31 11:17

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

    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.

    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.

    It a fix of a new feature in Bash 4.3.

    0 讨论(0)
提交回复
热议问题