title: Bash reading txt file and storing in array
body: <p>I'm writing my first Bash script, I have some experience with C and C# so I think the logic of the program is correct, it's just the syntax is so complicated because apparently there are many different ways to write the same thing!</p>
  
  <p>Here is the script, it simply checks if the argument (string) is contained in a certain file. If so it stores each line of the file in an array and writes an item of the array in a file. I'm sure there must be easier ways to achieve that but I want to do some practice with bash loops</p>
  
  <pre><code>#!/bin/bash
  
  NOME=$1
  c=0
  
  #IF NAME IS FOUND IN THE PHONEBOOK THEN STORE EACH LINE OF THE FILE INTO ARRAY
  #ONCE THE ARRAY IS DONE GET THE INDEX OF MATCHING NAME AND RETURN ARRAY[INDEX+1]
  
  if grep "$NOME" /root/phonebook.txt ; then
          echo "CREATING ARRAY"
          while read line
          do
                  myArray[$c]=$line # store line
                  c=$(expr $c + 1) # increase counter by 1
          done &lt; /root/phonebook.txt
  
  else
          echo "Name not found"
  fi
  
  c=0
  for i in myArray;
          do
                if   myArray[$i]="$NOME" ;  then
                   echo ${myArray[i+1]} &gt;&gt; /root/numbertocall.txt
                fi
  
  done
  </code></pre>
  
  <p>This code returns the only the second item of <code>myArray</code> (<code>myArray[2]</code>) or the second line of the file, why?</p>