1

I want to run several commands in parallel, but I have no idea why the following code piece does not work.

#!/bin/bash
( ping 8.8.8.8 )
( ping 192.168.0.1 )

It completely ignores the second ping command. Why is that so?

Edit: OK, now I know that you can run them in parallel by doing &.

ping 8.8.8.8 & ping 192.168.0.1

But why did the above code piece not work?

  • 1
    the first ping doesn't end by itself, so the 2nd command won't run until you kill the first ping... – Jeff Schaller Mar 5 '16 at 13:01
2

A subshell is not forking by default and you also would have to use & to send it to the background to execute the second subshell without waiting for the first subshell to end.

e.g.

ping 8.8.8.8 & ping 192.168.0.1
  • In most shells, a subshell forks and in any case, subshell or not, the shell will fork a process to execute that ping command in a separate process as there's no shell that I know that has ping as a builtin command. But the point here is that the shell will wait for the termination of that process. & doesn't make a difference whether the shell forks or not (it forks with and without &) but whether the shell will wait or not for that process to finish before carrying on with the rest of the script. – Stéphane Chazelas Sep 3 '18 at 21:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.