I have to multiply call an inflexible external tool that takes as arguments some input data and an output file to which it will write the processed data, for example:

```
some_prog() {  echo "modified_$1" > "$2"; }

```

For varying input, I want to call some\_prog, filter the output and write the output of all calls into the same file "out\_file". Additionally, I want to add a header line to the output file before each call of some\_prog. Given the following dummy filter:

```
slow_filter() { 
    read input; sleep "0.000$(($RANDOM % 10))"; echo "filtered_$input"
}

```

I wrote the following code:

```
rm -f out_file
for input in test_input{1..8}; do
    echo "#Header_for_$input"  >> "out_file"
    some_prog $input >( slow_filter  >> "out_file" )
done

```

However, this will produce an out\_file like this:

```
#Header_for_test_input1
#Header_for_test_input2
#Header_for_test_input3
#Header_for_test_input4
#Header_for_test_input5
#Header_for_test_input6
#Header_for_test_input7
#Header_for_test_input8
filtered_modified_test_input4
filtered_modified_test_input1
filtered_modified_test_input2
filtered_modified_test_input5
filtered_modified_test_input6
filtered_modified_test_input3
filtered_modified_test_input8
filtered_modified_test_input7

```

The output I expected was:

```
#Header_for_test_input1
filtered_modified_test_input1
#Header_for_test_input2
filtered_modified_test_input2
#Header_for_test_input3
filtered_modified_test_input3
#Header_for_test_input4
filtered_modified_test_input4
#Header_for_test_input5
filtered_modified_test_input5
#Header_for_test_input6
filtered_modified_test_input6
#Header_for_test_input7
filtered_modified_test_input7
#Header_for_test_input8
filtered_modified_test_input8

```

I realized that the >( ) process substitution forks the shell. Is there a way to synchronize the output of the subshells? Or is there another elegant solution to this problem? I want to avoid the obvious approach of writing to different files in each iteration because, in my code, the for loop has a few 100,000 iterations.