Bug 1460926

Summary: Indexing an Array in Bash is not working as expected
Product: Red Hat Enterprise Linux 7 Reporter: Siteshwar Vashisht <svashisht>
Component: bashAssignee: Siteshwar Vashisht <svashisht>
Status: CLOSED NOTABUG QA Contact: BaseOS QE - Apps <qe-baseos-apps>
Severity: unspecified Docs Contact:
Priority: unspecified    
Version: 7.4CC: dyordano, pstudeni, qe-baseos-apps
Target Milestone: rc   
Target Release: ---   
Hardware: Unspecified   
OS: Unspecified   
Whiteboard:
Fixed In Version: Doc Type: If docs needed, set a value
Doc Text:
Story Points: ---
Clone Of: 1392951 Environment:
Last Closed: 2017-06-19 14:27:33 UTC Type: Bug
Regression: --- Mount Type: ---
Documentation: --- CRM:
Verified Versions: Category: ---
oVirt Team: --- RHEL 7.3 requirements from Atomic Host:
Cloudforms Team: --- Target Upstream Version:
Embargoed:
Bug Depends On: 1392951    
Bug Blocks:    

Comment 2 Siteshwar Vashisht 2017-06-19 14:27:33 UTC
Anything between '[' and ']' is evaluated as a math expression. I will explain each command one by one :

'echo ${r[abc]}':

Here 'abc' will be replaced with 0, so it returns the 0th element from the array.

echo ${r[---abc]}:

Here math expression is evaluated as '-(--abc)'. Since 'abc' is 0, it will evaluate to '-(-1)' -> '1'. So it returns 1st element from array.

echo ${r[---abc]}:

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.