title: Bash looping through array - get index
body: <p>I have seen many answers to looping through an array in bash so that you not only get the index, but also the variable <em>at</em> that index. For example</p>
  
  <pre><code>for i in "${!foo[@]}"; do 
    printf "%s\t%s\n" "$i" "${foo[$i]}"
  done
  </code></pre>
  
  <p>However, for my variable <em>foo</em>, this is defined as:</p>
  
  <pre><code>foo=$(&lt; varlist.txt)
  </code></pre>
  
  <p>Where varlist.txt is a simple 10 row textfile consisting of a 4-letter code on each line. When I try the above method, the output is not matching up. What am I missing here?</p>
  
  <p>For example:</p>
  
  <p>vi varlist.txt</p>
  
  <pre><code> QOP1
   QOP2
   LMA1
   LMA2
   NKO1
   NKO2
   POZ1
   POZ2
   CCS1
   CCS2
  </code></pre>
  
  <p>vi file_to_test.txt</p>
  
  <pre><code> #text
   #more header text
   POZ1
   CCS2
  </code></pre>
  
  <p>The output should be: </p>
  
  <pre><code> 7 POZ1
   10 CCS2
  </code></pre>
  
  <p>As these are the 7th and 10th index from the varlist file.</p>