title: Export Bash command-line-argument variables for use in embedded Expect
body: <p>The <code>echo</code> test shows me my command line variables are working, but how do I pass them, export them, to be used in an embedded <code>expect</code>?</p>
  
  <pre><code>#!/bin/bash
  
  echo name of script is $0
  echo host is $1
  echo hostusername is $2
  echo hostpassword is $3
  
  expect -c 'spawn ssh -l $2 $1 &lt; ./sf-wall_scp_bash.sh
  sleep 2
  
  expect {
  "(yes/no)? " {send "yes\n"}
      exp_continue
  }
  
  expect {
  "?assword" {send "$3\r"}
  }
  sleep 1
  '
  </code></pre>
  
  <p>If invoked with the <code>expect</code> shebang, it works like</p>
  
  <pre><code>#!/usr/bin/expect
  
  set host [lindex $argv 0]
  set hostusername [lindex $argv 1]
  set hostpassword [lindex $argv 2]
  
  
  spawn ssh -l $hostusername $host
  sleep 2
  
  expect {
  "(yes/no)? " {send "yes\n"}
      exp_continue
  }
  
  expect {
  "?assword" {send "$hostpassword\r"}
  }
  sleep 1
  </code></pre>
  
  <p>...which works as expected. I need to use <code>bash</code> though and embed <code>expect</code> because the script is needing to call bash specific commands, functions, built in variables...otherwise simply using the second example would be a working option, but it is not.</p>
  
  <p>I've tried declaring and exporting the command line argument variables, and changing $1, $2, $3 in the first example above respectively to the variable names declared and exported, which works in another script I have in which I am actually declaring the variables within the script...the difference being they are not command line arguments.</p>
  
  <pre><code>host=$1
  export host
  hostusername=$2
  export hostusername
  hostpassword=$3
  export hostpassword
  </code></pre>
  
  <p>as well as just exporting them</p>
  
  <pre><code>export host
  export hostusername
  export hostpassword
  </code></pre>
  
  <p>and attempting the first example above. No change, <code>expect</code> continues to claim </p>
  
  <blockquote>
    <p>can't read "X": no such variable</p>
  </blockquote>
  
  <p>In another bash script I was able to successfully export variables by BOTH declaring them and then exporting them as in the example above. But I have not been able to have any such luck with passing <code>bash</code> command line argument variables to <code>expect</code></p>