title: What's the proper way to run commands on a remote machine by multi-layers of ssh
body: <p>Say there are 3 machine: 'local', 'A' and 'B'. Only 'local' and 'A' are connected and 'A' and 'B' are connected. And 'local' wants run commands on B via A.</p>
  
  <pre><code># I run ssh A "ssh B commands" frequently, so a function is created for have clearer code.
  run_on_B() {
    ssh A "ssh B \"$@\""
  }
  
  run_on_B 'echo export PATH=\\\$HOME/bin:\\\$PATH &gt;&gt; \$HOME/.bash_profile'
  </code></pre>
  
  <p>Is there a better way to run commands on a remote machine via ssh? I feel it is easy to make mistake to do escaping <code>$HOME</code> or <code>$PATH</code> here.</p>