title: How to access last item in bash array on Mac OS?
body: <p>I had to create a bash array on Mac OS as follows. The <code>$1</code> represents # of git commits you want to store in the array.  </p>
  
  <pre><code>IFS=$'\n' read -rd '' -a array&lt;&lt;&lt; "$(git log -n $1 | grep commit |  awk '{print $2}')"
  </code></pre>
  
  <p>I can't access last array item as <code>${array[-1]}</code>. I get the error "array: bad array subscript".</p>
  
  <p>However, when I create the array on linux OS, I can access the last array item in the same way successfully. </p>
  
  <pre><code>readarray -t array &lt;&lt;&lt; "$(git log -n $1 | grep commit |  awk '{print $2}')"
  </code></pre>
  
  <p><code>echo ${array[-1]}</code> is successful on Linux machine but not on Mac OS machine. </p>