If do this on my bash terminal:
(
(sleep 0.5 && echo 'first command, second result' &);
(echo 'second command, first result' &);
)
then I see:
second command, first result
myprompt$
first command, second result
Now, I want to wait for all of my subshells to finish.
How do I achieve that?
I know that I could add wait at the end of each subshell command:
(
(sleep 0.5 && echo 'first command, second result' & wait);
(echo 'second command, first result' & wait)
)
first command, second result
second command, first result
myprompt$
and seemingly I get my result (I don't, as the order is reversed, and I think the subshells are not waited upon).
I also thought I could do this:
(
(sleep 0.5 && echo 'first command, second result') &
(echo 'second command, first result') &
wait
)
second command, first result
first command, second result
prompt$
but this is accidentally changing the CONTENT of the subshell.
I actually want to be able to wait for this as well:
(
(sleep 0.5 && echo 'first command, second result' &) &
(echo 'second command, first result' &) &
wait
)
second command, first result
prompt$
first command, second result
So this too won't wait properly.
My example here is contrived; in reality I do not control the bash script that my outer subshell may execute.
The bash scripts gets dynamically constructed and it should be as forgiving to improper bash code as possible; at least for now.
The outer shell, if possible, should be able to determine if the subshells have ended.
Is this possible, and if so how?
I can freely control the outer shell and add any command there. I cannot modify the code within the subshells.