title: Passing variable to `expect` in bash array
body: <p>I am trying to use a FOR loop to iterate over IP addresses (in a bash array), logs in, runs a script and then exits. The array is called ${INSTANCE_IPS[@]}. The following code doesn't work though, as <code>expect</code> doesn't seem to be able to accept the variable $instance.</p>
  
  <pre><code>for instance in ${INSTANCE_IPS[@]}
    do
    echo $instance
    /usr/bin/expect -c '
    spawn ssh root@$instance;
    expect "?assword: ";
    send "&lt;password&gt;\r";
    expect "# ";
    send ". /usr/local/bin/bootstrap.sh\r";
    expect "# ";
    send "exit\r" '
  done
  </code></pre>
  
  <p>However, <code>expect</code> complains with:</p>
  
  <pre><code>can't read "instance": no such variable
      while executing
  "spawn ssh root@$instance"
  </code></pre>
  
  <p>There is another question on stackoverflow located <a href="https://stackoverflow.com/questions/21928226/how-to-pass-shell-script-variable-into-expect-script">here</a>, that uses environmental variables to achieve this, however it doesn't allow me to iterate through different IP addresses like I can in an array.</p>
  
  <p>Any help is appreciated.</p>
  
  <p>Cheers</p>