<p>The basic interface for entering commands on a Linux system is a <em>shell</em>. It is a process that enables you to enter a command directly, or to specify a file (&#34;script&#34;) that contains a sequence of commands to be executed. Shells are organized hierarchically: any shell can create a new shell, and the new shell is considered a <em>child</em> process of the creating (<em>parent</em>) shell.</p><p>By default a child process is dependent on its parent in the sense that if the parent process terminates, the child also terminates. And output (stdout and stderr) is passed back from the child to the parent process.</p><p>In a Bash shell script you create a subshell using the parenthesis notation:</p><p><code><em></em></code></p><em><code><pre> #!/bin/bash echo &#34;Before starting subshell&#34; ( count&#61;1 while [ $count -le 99 ] do echo &#34;$count&#34; sleep 1 (( count&#43;&#43; )) done ) echo &#34;Finished&#34; </pre></code></em><code></code><p>In the above example the <a href="https://www.lifewire.com/write-bash-while-loops-2200576" data-component="link" data-source="inlineLink" data-type="internalLink" data-ordinal="1">while loop</a> is enclosed in parenthesis, which causes it to be executed in a subshell, which is a child process of the shell in which the script file is executed.</p><p>Without specifying that the subshell is to be executed in the background, the parent shell waits for the subshell to finish before continuing with rest of the script (following the subshell expression).</p><p>This means, if you want to run subshells in parallel you have to run them in the background, which is accomplished as usual with the &#34;&amp;&#34; (ampersand) character following the subshell expression:</p><p><code><em></em></code></p><em><code><pre> #!/bin/bash echo &#34;Before starting subshell&#34; ( count&#61;1 while [ $count -le 99 ] do echo &#34;$count&#34; sleep 1 (( count&#43;&#43; )) done ) &amp; echo &#34;Finished&#34; </pre></code></em><code></code><p>So if you create multiple subshells as background <a href="https://www.lifewire.com/multitasking-background-foreground-process-2180219" data-component="link" data-source="inlineLink" data-type="internalLink" data-ordinal="2">processes</a>, you can run tasks in parallel. In general, the operating system will use different processors (cores) for each process and subprocess, assuming there are at least as many processors/cores as there are processes. Otherwise tasks will be assigned to the same processors/cores. In that case the processor/core continuously switches between the assigned tasks (processes) until they are completed. For example:</p><p><code><em></em></code></p><em><code><pre> #!/bin/bash echo &#34;Before starting subshell&#34; ( count&#61;1 while [ $count -le 99 ] do echo &#34;$count&#34; sleep 1 (( count&#43;&#43; )) done ) &amp; ( count&#61;1000 while [ $count -le 1099 ] do echo &#34;$count&#34; sleep 1 (( count&#43;&#43; )) done ) &amp; echo &#34;Finished&#34; </pre></code></em><code></code><p>Now we have two subprocesses. The first one counts from 1 to 99, and the second one from 1000 to 1099.</p><p>You can use the <a href="https://www.lifewire.com/linux-command-wait-4092521" data-component="link" data-source="inlineLink" data-type="internalLink" data-ordinal="3">wait</a> statement to tell the parent process to wait for the subprocesses to finish before proceeding with the rest of the script:</p><p><code><em></em></code></p><em><code><pre> #!/bin/bash echo &#34;Before starting subshell&#34; ( count&#61;1 while [ $count -le 99 ] do echo &#34;$count&#34; sleep 1 (( count&#43;&#43; )) done ) &amp; ( count&#61;1000 while [ $count -le 1099 ] do echo &#34;$count&#34; sleep 1 (( count&#43;&#43; )) done ) &amp; wait echo &#34;Finished&#34; </pre></code></em><code></code><p>Subshells are also useful when commands need to be executed in a particular environment (variable settings) or directory. If each command is executed in a different subshell there is no risk variable settings will be mixed up, and on completion the settings and the current directory don&#39;t need to be restored, as the environment of the parent process is not affected by any of its subprocesses.</p><p>Subshells can be used in <a href="https://www.lifewire.com/pass-arguments-to-bash-script-2200571" data-component="link" data-source="inlineLink" data-type="internalLink" data-ordinal="4">function definitions</a> so that they can be executed multiple times, with different parameters if needed.</p>