title: Negative subscripts in Bash arrays
body: <p>After creating a simple bash stack script I encountered an error related to bash arrays.</p>
  
  <p><em>From the Bash manual:</em></p>
  
  <blockquote>
    <p>An indexed array is created automatically if any variable is assigned
    to using the syntax:</p>
    
    <p><code>name[subscript]=value</code></p>
    
    <p>name[subscript]=value The subscript is treated as an arithmetic
    expression that must evaluate to a number. If subscript evaluates to a
    number less than zero, it is used as an offset from one greater than
    the array’s maximum index (so a subcript of -1 refers to the last
    element of the array).</p>
  </blockquote>
  
  <p>Everything worked perfectly fine on my local machine unfortunately my server environment is slightly different.</p>
  
  <p>The negative subscript worked on Ubuntu 14.04 / GNU bash version 4.3.11(1). However when I tried to run the same script on my server with GNU bash version 4.2.46(1) I encountered the following error:</p>
  
  <pre><code>line 46: [-1]: bad array subscript 
  </code></pre>
  
  <p>I tried to create an array and call the last element directly from the terminal (not inside the script) and it worked:</p>
  
  <pre><code>user@local:~$ arr=( 1 2 3 4 5 )
  user@local:~$ echo ${arr[-1]}
  5
  </code></pre>
  
  <p>I am kinda confused here. Is there any solution or workaround for this?</p>