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?

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

1 Answer 1

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
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. Commented Sep 3, 2018 at 21:30

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.