title: Bash - Escaping SSH commands
body: <p>I have a set of scripts that I use to download files via FTP and then delete them from the server.</p>
  
  <p>It works as follows:</p>
  
  <pre><code>for dir in `ls /volume1/auto_downloads/sync-complete`
  do
  if [ "x$dir" != *"x"* ]
  then
  echo "DIR: $dir"
  
  echo "Moving out of complete"
          # Soft delete from server so they don't get downloaded again
          ssh dan@172.19.1.15 mv -v "'/home/dan/Downloads/complete/$dir'" /home/dan/Downloads/downloaded
  </code></pre>
  
  <p>Now $dir could be "This is a file" which works fine.</p>
  
  <p>The problem I'm having is with special characters eg:</p>
  
  <ul>
  <li>"This is (a) file"</li>
  <li>This is a file &amp; stuff"</li>
  </ul>
  
  <p>tend to error:</p>
  
  <pre><code>bash: -c: line 0: syntax error near unexpected token `('
  bash: -c: line 0: `mv -v '/home/dan/Downloads/complete/This is (a) file' /home/dan/Downloads/downloaded'
  </code></pre>
  
  <p>I can't work out how to escape it so both the variable gets evaluated and the command gets escaped properly. I've tried various combinations of escape characters, literal quotes, normal quotes, etc</p>