bash: put list files into a variable and but size of array is 1
You're not creating an array unless you surround it with (
)
:
dirlist=(`ls ${prefix}*.text`)
Declare an array of files:
arr=(~/myDir/*)
Iterate through the array using a counter:
for ((i=0; i < ${#arr[@]}; i++)); do
# [do something to each element of array]
echo "${arr[$i]}"
done
This:
dirlist=`ls ${prefix}*.text`
doesn't make an array. It only makes a string with space separated file names.
You have to do
dirlist=(`ls ${prefix}*.text`)
to make it an array.
Then $dirlist
will reference only the first element, so you have to use
${dirlist[*]}
to reference all of them in the loop.