I would like to create and fill an array dynamically but it doesn't work like this:
i=0
while true; do
read input
field[$i]=$input
((i++))
echo {$field[$i]}
done
Try something like this:
#! /bin/bash
field=()
while read -r input ; do
field+=("$input")
done
echo Num items: ${#field[@]}
echo Data: ${field[@]}
It stops reading when no more input is available (end of file, ^D in the keyboard), then prints the number of elements read and the whole array.
i= field=()
while :; do
read -r 'field[i++]'
done
Is one way. mapfile is another. Or any of these. However what you've posted is valid.
echothere. You just have an infinite loop that appends an array and does nothing with it. Yes expanding"$test"is always equivalent to"${test[0]}"(even iftestisn't an array).