'Bash: Append 'find'-results to indexed array using loop [duplicate]

I have the following code which isn't working:

declare -a pids
find /foo/bar/ -iname "*.pid" -print0 | while read -d $'\0' file
do
     pids+=("$file")
done

for i in "${pids[@]}"
do
     echo $i
done

It should create an indexed array, search the given folder for all PIDs and append them to the array using a while loop however it seems that nothing is happening.

For me it looks like the results of find don't get appended to the array because the for loop at the bottom doesn't echo anything. I'm not sure if the += operant is correct for this but looking at similar question it seems to be. When trying to manually append a value to the array pids, instead of creating a new value on the next index it just adds the string to the first value (index 0).

For example this:

declare -a pids
pids+="foo1"
pids+="foo2"

results in this:

pids[0]="foo1foo2"

instead of:

pids[0]="foo1"
pids[1]="foo2"

Is += not the right operant for this or is something else wrong?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source