Let say I have an array $Info whose elements are ;

!    total energy              = -1090.13343774 Ry
!    total energy              = -1090.20757070 Ry
!    total energy              = -1090.24296462 Ry
!    total energy              = -1090.25563488 Ry
!    total energy              = -1090.27085564 Ry
!    total energy              = -1090.27693129 Ry
!    total energy              = -1090.28213580 Ry
!    total energy              = -1090.29131927 Ry

and I want to take its last element , split it with spaces and give to an another array $LastInfo. How can I do that ?

I tried this;

lst=$((${#Info[@]} -1))
IFS=' ' LastInfo=($en[$lst])

But as posted in here the code doesn't work.

  • That is actually still the same problem as your last question. Commented Jun 4, 2016 at 5:26
  • 3
    Possible duplicate of What is wrong in this code? Commented Jun 4, 2016 at 5:27
  • In last question I asked the mistake in the code but in this one , I am asking how can I do this differently.
    – Our
    Commented Jun 4, 2016 at 5:39
  • 1
    It's the same issue and you're referring to the same code. Your main error is to think that space delimitation wouldn't count the number of spaces in between the columns. Commented Jun 4, 2016 at 5:41
  • 1
    Indeed, IFS describes a list of characters to be used as separators, not a string to use. You need a better approach such as column position since your input appears to be fixed. Look at man cut. Commented Jun 4, 2016 at 6:04

3 Answers 3


With ksh93, bash or zsh

last=( "${arr[@]: -1}" )

That has the advantage over the zsh-style last=( "${arr[-1]}" ) (beside being more portable: to ksh93 and to bash 4.2 or older which don't support the zsh-style syntax) that if $arr is an empty array, $last becomes an empty array as well rather than an array containing one empty element.

  1. If your array is called Info, and you're calculating the index of the last element of Info, it doesn't make sense to use it to index into en (a different array).
  2. When you do index into an array, you have to use braces;

What you probably want to do is:

# given that $Info is a populated array..

Whitespace is already a default separator, so each space-separated element of the list entry in the Info array will be an entry in the new LastInfo array.

  • Even with this I still taking the first [0] element of $Info
    – Our
    Commented Jun 4, 2016 at 6:05
  • @DopeGhoti: Um, what?  ${#Info[@]} is the number of elements in Info.  And ${Info[-1]} doesn't work in bash. Commented Jun 4, 2016 at 6:10
  • I didn't notice the octothorpe in the ${#Info[@]} construct. And I just tested ${array[-1]} in bash and it worked? See hastebin.com/umedapaqoy.sh
    – DopeGhoti
    Commented Jun 4, 2016 at 6:15
  • @Scott negative-subscript-from-end is a newish feature; in my 4.3.11+ on Ubuntu /usr/share/doc/bash/NEWS.gz lists it as a change in 4.3 from 4.2. Commented Jun 4, 2016 at 7:57

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .