This is the expected behavior. readarray will create an array in which each element of the array will be a line in the input.
If you want to see the whole array, you need to use
echo "${myarray[@]}"
as echo "$myarray will only output myarray[0] , and ${myarray[1]} is the second row of data.
What you are looking for is a two-dimensional array. See for example.
If you need an array with the contents of the first row, you can do like this:
$ read -a arr < demo.txt $ echo ${arr[0]} 1 $ echo ${arr[1]} 2 $ echo ${arr[2]} 3
source share