2

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?

Jeff Schaller
  • 67,283
  • 35
  • 116
  • 255
mkdrive2
  • 662

1 Answers1

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
Thomas
  • 6,362
  • 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 03 '18 at 21:30