On Wed, Nov 23, 2011 at 08:41:56AM +0100, ale...@pasta.net wrote: > Description: > It looks as if bash is not waiting for a process-substition process > which is reading stdin to complete before bash moves on to executing > the next statement.
Process substitutions are background processes. They are exactly the same as doing: mkfifo tmp-fifo your job <tmp-fifo & disown some other job >tmp-fifo > spew_and_slurp_with_lock() > { > local I > > for ((I=0; I<1000; I++)); do > echo "some junk" > done > >(mkdir $LOCK_DIR; cat > /dev/null; rmdir $LOCK_DIR) > } In this case, the entire mkdir;cat;rmdir job is launched in the background, with a file descriptor pointing to it. The for loop is being redirected to that. The for loop should block until the cat command reads from the pipe, at which point the main shell may move on. Meanwhile, the cat command which is in the background does its processing, and terminates; and then finally rmdir gets called. So yes, the main shell and the rmdir could be doing things simultaneously. That's how process substitutions are defined. If you want to ensure that the main shell waits for the rmdir, then you must abandon the process substitution syntax and use your own explicit FIFOs: mkfifo tmp-fifo { mkdir "$LOCK_DIR"; cat >/dev/null; rmdir "$LOCK_DIR"; } < tmp-fifo & pid=$! for ... done > tmp-fifo wait $pid rm tmp-fifo Only then will you be assured that things will run the way you need them to.