Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get last element in Bash array

Tags:

arrays

bash

shell

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

My research shows:

Bash-4.3-rc1 available for 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.

And Bash manual 4.3, on Arrays

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.

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

like image 977
fedorqui 'SO stop harming' Avatar asked May 02 '16 08:05

fedorqui 'SO stop harming'


People also ask

How do you extract the last element of an array?

1) Using the array length property The length property returns the number of elements in an array. Subtracting 1 from the length of an array gives the index of the last element of an array using which the last element can be accessed.

How do I get the last index of an array in bash?

To get the last element (5) from the array, we can use the subscript [ ] syntax by passing an index -1 . In bash the arrays are zero-indexed. The first element index is 0 and negative indices counting back from the end of an array, so the index of -1 is used to access the last element.

What is the last element in an array?

The Last element is nothing but the element at the index position that is the length of the array minus-1. If the length is 4 then the last element is arr[3].

What is $_ in bash?

$_ (dollar underscore) is another special bash parameter and used to reference the absolute file name of the shell or bash script which is being executed as specified in the argument list. This bash parameter is also used to hold the name of mail file while checking emails.


1 Answers

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.

like image 72
SLePort Avatar answered Sep 26 '22 03:09

SLePort