title: Why is my array gone after exiting loop?
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/11656013/bash-cant-build-array-in-right-side-of-pipe">Bash: can’t build array in right side of pipe</a>  </p>
  </blockquote>
  
  
  
  <p>I need to create an array from a file. I have to calculate an index where to store data. When I get outside my read loop, my arrays seems to be gone. Why???</p>
  
  <p>The script below illustrates the problem.</p>
  
  <pre><code>#!/bin/bash
  
  echo -e "15\n21\n33" | while read i ; do
      ar[$i]="set"
      echo ${!ar[@]}
      echo ${ar[@]}
    done
  
  echo
  echo outside loop:  
  echo ${!ar[@]} 
  echo ${ar[@]} 
  </code></pre>
  
  <p>As you can see in the output, printing the array indexes and content no longer work outside of loop.</p>
  
  <pre><code>15
  set
  15 21
  set set
  15 21 33
  set set set
  
  outside loop:
  </code></pre>