Subshells and parallel processing
We already talked a bit about in the opening chapters of this book; they can be defined as child processes of their main shell. So, a subshell is a command interpreter inside a command interpreter. When does this happen? Well, usually when we run a script, this spawns its own shell and there executes all the commands listed; but notice this nice detail: an external command, unless invoked using exec
, spawns a subprocess, but a builtin doesn't. And this is the reason why the bultins execution time is faster than the execution time for the corresponding external command, as we saw in the previous pages of this book.
Well, what can be useful for a subshell? Let's see a small example that will make everything easier:
#!/bin/bash echo "This is the main subshell" (echo "And this is the second" ; for i in {1..10} ; do echo $i ; done)
Nothing special. We echo in the first subshell spawned by the script, and then open a subshell from inside the subshell and echo...