When I declare an array in bash $ ARRAY=('ele1' ele2') I can append an element to it with $ ARRAY+=('ele3').
echo ${ARRAY[@]}
ele1 ele2 ele3
However, inside a script in a while loop I don't get it to work:
FOUNDFILES=$(ls -lA)
LINE_CNT=1
ARRAY=()
echo -e "$FOUNDFILES" | while read line
do
ARRAY+=("test")
LINE_CNT=$((LINE_CNT+1))
done
echo "${ARRAY[@]}"
echo $LINE_CNT
LINE_CNT variable delivers the amount of files that were found, but my array stays empty.
What am I doing wrong?
echo "${FILE_ARRAY[@]}"instead ofecho "${ARRAY[@]}", maybe that lead you to believe thatARRAYis empty.while ... done < <(echo -e "$FOUNDFILES")isntead.echo -eare both unsafe ways to handle the file list. See my answer here for a safe way to do it.while ... \ndo \ndoSomething \ndone < <(echo -e "$FOUNDFILES")