title: using expect inside a bash script function
body: <p>I know this question is already asked trillion times in past. However my question is slightly different from the title.   </p>
  
  <p>I want to put the below chunk of code inside a function , I am not clear how I am supposed to do.  </p>
  
  <pre><code>user&gt;cat myScript.sh  
  
  myFunction() {  
  /usr/bin/expect  &lt;&lt;EOF    
  #!/usr/bin/expect -f  
  spawn ssh user@$1  
  expect "assword:"  
  send "mypassword\r"  
  interact  
  EOF   
  
  }  
  
  echo -e "Enter the server name"  
  read servername  
  
  myFunction servername
  </code></pre>
  
  <p>-----------Script ended---------------    </p>
  
  <p>I am aware of ssh keys and I can do the same using it, but I do not have authority to use ssh keys   as I am using a shared userID on the local server.     </p>
  
  <h1>Problem:</h1>
  
  <p>This script is not able to skip the password promt and login is getting failed like below:    </p>
  
  <pre><code>[user~]$ bash -x login.sh  
  + /usr/bin/expect  
  spawn ssh testUser@1.2.3.4  
  testUser@1.2.3.4's password:  
  [user~]$
  </code></pre>