title: I/O redirection in ssh command
body: <p>I wrote a bash script <code>rm_remote_file.sh</code> that ssh to a set of remote machines and delete a file. I used <code>&amp;</code> at the end of each function call to run these command in parallel, the script looks like this:</p>
  
  <pre><code>#!/bin/bash
  
  rm_remote_file() {
      echo "Removing file on node $1:"
      ssh $1 'rm ~/test_file'
  }
  
  for node in node1.com node2.com node3.com; do
      rm_remote_file $node &amp;
  done
  </code></pre>
  
  <p>When test_file exists on each node - the <code>rm</code> command succeeds - the output of this script is:</p>
  
  <pre><code>Removing file on node node1.com:
  Removing file on node node2.com:
  Removing file on node node3.com:
  </code></pre>
  
  <p>I prefer having each hostname printed out. However, if the test_file does not exist on each node - the <code>rm</code> command fails - the output of this script is:</p>
  
  <pre><code>rm: cannot remove ‘~/test_file’: No such file or directory
  rm: cannot remove ‘~/test_file’: No such file or directory
  rm: cannot remove ‘~/test_file’: No such file or directory
  </code></pre>
  
  <p>So the printing of the nodes' host names are suppressed by this error message. I think this behavior has something to do with the I/O redirection and using things such as <code>2&gt;&amp;1</code> can solve the issue. But I would like to know why ssh command error message would suppress the echo command. </p>
  
  <p>Note that this only happens with ssh command, the following script, which just removes some local files, would outputs both "Removing file" and the "No such file or directory".</p>
  
  <pre><code>#!/bin/bash
  
  rm_file() {
      echo "Removing file..."
      rm ./$1
  }
  
  for file in test1 test2 test3 test4 test5; do
      rm_file $file &amp;
  done
  </code></pre>