Sample Header Ad - 728x90

Closing stdout required for wait on sub process to finish?

0 votes
0 answers
381 views
I have a question from https://mywiki.wooledge.org/BashFAQ/106 Have a look at this code:
exec > >(tee myfile)
pspid=$!

# ... stuff ...
echo "A"
cat file
echo "B"
# end stuff

# All done.  Close stdout so the proc sub will terminate, and wait for it.
exec >&-
wait $pspid


# what happens if we delete line exec >&- ?
# what if ...stuff... do not finish before >&- ?
My first question is why do we need exec >&- here? What happens if we delete it? I'm guessing deleting exec >&- will cause wait $pspid to wait indefinitely. Since tee myfile runs in background asynchronously I would expect stuff will be processed out of order. So my second question is, what if any of stuff do not finish before >&-?
Asked by Logan Lee (249 rep)
Feb 16, 2021, 02:35 AM