I pipe the same content to multiple commands with tee
, redirects and process substitution subshells like this:
#!/usr/bin/env bash
echo 'hello' | tee \
>( sleep 3; cat /dev/stdin ) \
>( sleep 2; cat /dev/stdin ) \
>( sleep 1; cat /dev/stdin )
wait # Doesn't work :(
However, what I see is that the process substitution subshell output is written to the terminal after the main script exits and wait
doesn't work:
$ ./test.sh
hello
$ hello
hello
hello
How to properly wait for the process substitution subshells?
Best Answer
In
bash
, you can't wait for process substitution. In:the whole command finish as soon as
cmd1
finish, regardless the status ofcmd2
.You have to implement a mechanism to signal the parent process that the
cmd2
have finished. An easy way, using a fifo: