0

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?

Icoryx
  • 96
  • 7
  • Use process substitution instead of a pipe, see the `find` duplicate for that; the other duplicate explains why you can't use a pipe. – Benjamin W. Feb 09 '22 at 15:24
  • [BashFAQ #24](https://mywiki.wooledge.org/BashFAQ/024): *I set variables in a loop that's in a pipeline. Why do they disappear after the loop terminates? Or, why can't I pipe data to read?* – Charles Duffy Feb 09 '22 at 15:28

0 Answers0