Andrew Andrew - 3 months ago 6
Bash Question

Send process' STDOUT to screen and second process, but wait for both to finish

This is a sort of branch from this question (specifically avoiding the race condition pointed out by jmb): How can I send the stdout of one process to multiple processes using (preferably unnamed) pipes in Unix (or Windows)?

I want to execute a process in such a way that its output is displayed on the screen plus piped to a second process, but that my terminal is not released until both processes are complete. For example, if I had:

foo.sh

echo "FOO"


bar.sh:

read inp
sleep 5
echo "BAR${inp}BAR"


And then ran:

foo.sh | tee >(bar.sh)


I'd like to get the output:

FOO
BARFOOBAR
> <--terminal released after bar (total time 5s)


but instead I get:

FOO
>BARFOOBAR <--terminal released after foo in ~0s, before bar.sh returns at 5s


Is there an easy way to do this? The only way I can think of right now is to instead call everything like this:

foo.sh | bar.sh


and then rewrite bar.sh such that it continually checks its STDIN and prints this to screen, but this seems like a very inelegant way of solving the problem.

Thanks for the help!

Answer

try:

  foo.sh | tee /dev/tty | bar.sh
Comments