title: Have remote shell evaluate expressions inside expect ssh script
body: <p>I have to use <em>expect</em> and <em>ssh</em> for automating on a remote shell (<em>bash</em> shell locally and remotely in my particular case). That is, I need to wrap <code>ssh someuser@example.com "echo This is \$(hostname)"</code> inside an <code>expect</code> script.</p>
  
  <p>Running the above "manual" script, I get the expected output: <code>This is example.com</code>, so the <code>$(hostname)</code> expression (<em>command substitution</em>) gets evaluated on the remote machine.</p>
  
  <p>Now I wrapped that ssh-remote-shell command inside an <code>expect</code> <em>here document</em>:</p>
  
  <pre><code>#/bin/bash
  
  expect &lt;&lt;- DONE
    spawn ssh someuser@example.com "echo This is \\$(hostname)"
  DONE
  </code></pre>
  
  <p>The wrapped script returns <code>[...] This is localhost</code> instead. So the <code>$(hostname)</code> expression gets evaluated on my local machine, not on the remote machine.</p>
  
  <p>I've tried different levels of backslash escaping, single quotes and double quotes, <code>&lt;&lt;- DONE</code> and <code>&lt;&lt; DONE</code>, and moving <code>$(hostname)</code> to its own <em>expect</em> variable (i.e., <code>set someCommand "\\$hostname"</code>, then referencing <code>$someCommand</code>). But nothing helped.</p>
  
  <p>How can I have the remote shell evaluate shell expressions in SSH <em>expect</em> scripts?</p>