title: How to send a password with a $ symbol in Expect Script
body: <p>I have an expect script that I need to login to a remote system and execute commands. This script works with the exception of providing the password to the root account. The root password contains a dollar sign that I cannot seem to get to work. Here is the code</p>
  
  <pre><code>#!/usr/bin/expect
  set timeout 3
  set username "root"
  set password "Pas$word"
  set hostname [lindex $argv 0]
  log_user 0
  
  send_user "\n#####\n# $hostname\n#####\n"
  
  spawn ssh -q -o StrictHostKeyChecking=no $username@$hostname
  
  expect {
      timeout { send_user "\nFailed to get password prompt\n"; exit 1 }
      eof { send_user "\nSSH failure for $hostname\n"; exit 1 }
      "*assword"
  }
  
  send "$password\r"
  
  expect {
      timeout { send_user "\nLogin failed. Password incorrect.\n"; exit 1}
      "*\$ "
  }
  
  send_user "\nPassword is correct\n"
  
  expect "$ " { send "ls" }
  </code></pre>
  
  <p>I have verified this works when providing credentials whose passwords don't contain the dollar sign, but I can't get it to work with the root account. It always produces the <code>Login failed. Password incorrect</code> timeout error. Changing the password is not an option. I have tried to supply the <code>\</code> escape character in the password definition like so:</p>
  
  <pre><code>set password "Pas\$word"
  </code></pre>
  
  <p>And I get the same results... any ideas on what I'm doing wrong?</p>
  
  <p>Thanks</p>
  
  <p><strong>EDIT</strong>
  As I said. I already tried to escape the $ character. But to clarify, I added a print statement for the password when the script starts up to verify the variable contains the password correctly... Here is the change:</p>
  
  <pre><code>set password "Pas\$word"
  ...
  send_user "\n#####\n# $hostname\n#####\n"
  send_user "Using password: $password\n"
  ...
  </code></pre>
  
  <p>Here is the console output:</p>
  
  <pre><code>njozwiak@ubuntu:~$ ./ssh_ls.sh 192.168.5.93
  
  #####
  # 192.168.5.93
  #####
  Using password: Pas$word
  
  Login failed. Password incorrect.
  </code></pre>