Skip to main content
Became Hot Network Question
edited tags
Link
Kusalananda
  • 356.2k
  • 42
  • 737
  • 1.1k
Post Reopened by k0pernikus, Stephen Kitt bash
added 626 characters in body
Source Link
k0pernikus
  • 16.7k
  • 22
  • 64
  • 82

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 cannotalso thought I could do thatthis:

(
  (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

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.

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 cannot do that as 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.

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.

Post Closed as "Duplicate" by Stephen Kitt bash
added 85 characters in body
Source Link
k0pernikus
  • 16.7k
  • 22
  • 64
  • 82

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 cannot do that as 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.

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 cannot do that as 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.

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 cannot do that as 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.

Source Link
k0pernikus
  • 16.7k
  • 22
  • 64
  • 82
Loading