title: Bash while loop stops after first iteration when subshell is called
body: <p>This contrived bash script demonstrates the issue. </p>
  
  <pre><code>#!/bin/bash
  while read -r node ; do
      echo checking $node for Agent;
         PID=$(ssh $node ""ps -edf | grep [j]ava | awk '{print $2}'"")
         echo $PID got to here.
  done &lt; ~/agents_master.list
  </code></pre>
  
  <p>agents_master.list contains 1 server per line:</p>
  
  <pre><code>server1
  server2
  server3
  </code></pre>
  
  <p>Which only outputs the following:</p>
  
  <pre><code>checking server1 for Agent
  Authorized use only
  25176 got to here
  </code></pre>
  
  <p>Server 2 and 3 aren't even echoed out to screen by the line <code>echo checking $node...</code></p>
  
  <p>If I comment out the line <code>PID=$(....</code> then the while completes the whole agents_master.list file correctly... </p>
  
  <pre><code>checking server1 for Agent
  got to here
  checking server2 for Agent
  got to here
  checking server3 for Agent
  got to here
  </code></pre>
  
  <p>From the googling I've done, it sounds like this is related to the subshell that <code>$(...)</code> creates, but I don't understand why it is causing the loop to stop at the first server, <code>server1</code>.</p>
  
  <p>Yes, this code could be re-written but I'm keen to understand this behaviour of bash and why this is happening for future. </p>