title: Expect script for rsync with quotation marks
body: <p>I want to rsync a folder to another machine. sshpass is not an option and certificates don't either. So I'm stuck with expect.</p>
  
  <p>So far, I have this bash script (minimized).</p>
  
  <pre><code>read -p "Enter remote IP: " IP
  read -p "Enter remote user: " USER
  read -s -p "Enter remote password: " PASSWORD
  expect &lt;&lt;EOF
      set timeout -1
      spawn rsync -e "ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null" \
          -aHv --exclude=".*/" --delete ../.. ${USER}@${IP}:~/destination
      expect "password: "
      send "${PASSWORD}\n"
      expect eof
  EOF
  </code></pre>
  
  <p>Problem is that rsync doesn't respect the "--exclude" option.
  In the stdout, I see:</p>
  
  <pre><code>spawn rsync -e ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -aHv --exclude=".*/" --delete ../.. pi@10.0.1.2:~/OberonHAP
  </code></pre>
  
  <p>Somehow, the quotes around the -e option vanished, but exclude still has them.</p>
  
  <p>So I escape the quotes</p>
  
  <pre><code>expect &lt;&lt;EOF
      set timeout -1
      spawn rsync -e \"ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null\" \
          -aHv --exclude=".*/" --delete ../.. ${USER}@${IP}:~/destination
      expect "password: "
      send "${PASSWORD}\n"
      expect eof
  EOF
  </code></pre>
  
  <p>but now I get </p>
  
  <pre><code>Missing trailing-" in remote-shell command.
  </code></pre>
  
  <p>Note that the command itself works perfectly when typed directly into a Terminal.</p>
  
  <p>I've also tried different things, like 'EOF' heredoc and passing in the variables via environment, but always get error messages of different sorts :-)</p>
  
  <p><strong>How do I have to escape the rsync command so that it works fine?</strong></p>