Red Hat Bugzilla – Bug 1460926
Indexing an Array in Bash is not working as expected
Last modified: 2017-06-19 10:27:33 EDT
Anything between '[' and ']' is evaluated as a math expression. I will explain each command one by one :
Here 'abc' will be replaced with 0, so it returns the 0th element from the array.
Here math expression is evaluated as '-(--abc)'. Since 'abc' is 0, it will evaluate to '-(-1)' -> '1'. So it returns 1st element from array.
It's the same as previous expression, but 'abc' is set to '-1' due to previous command. It will evaluate to '-(-2)' -> '2'. It returns empty element because array does not have a second element.
This is not a bug in bash.