title: Expect script: to perform actions after closing ssh
body: <p>The question is to preserve a variable and to perform actions after closing ssh within expect script inside bash.</p>
  
  <p>This is what I`ve got so far:</p>
  
  <pre><code>echo "Getting package name..."
  
  getPackageName=$(expect -c '
  exp_internal 1
  log_user 1
  global expect_out
  
  # puts "Getting package name..."
  
  spawn ssh -q -o StrictHostKeyChecking=no -o PreferredAuthentications=password -o PubkeyAuthentication=no -o RSAAuthentication=no -l user 10.20.30.40
  sleep 1
  
  expect {
      "*sword*" {
          send "12341234\r"
      }
      timeout {
          send_user "Error: timeout\n"
          exit 1
      }
  }
  
  expect {
      "*user@*&gt;*" {
          # getting name of the latest modified file
          send "cd /export/home/user/Releases/build/1.3.32.0 &amp;&amp; find * -type f -printf '"'"'%T@ %p\\n'"'"' | sort -n | tail -1 | cut -f2- -d\" \"\r"
      }
      timeout {
          send_user "Error: timeout\n"
          exit 1
      }
  }
  
  expect {
      "BUILD_MAIN*" {
          # assigning value to variable
          set result_lines [split $expect_out(0,string) \r\n]
          set package_filename [lindex $result_lines 0]
          puts "package_filename: $package_filename"
      }
      timeout {
          send_user "Error: timeout\n"
          exit 1
      }
  }
  
  expect "*#"
  send "exit\r"
  
  # here I need to perform some actions on local machine after ssh logout
  expect "Connection*"
  send "export LATEST_BUILD=$package_filename\r"
  send_user "Message sent to user"
  
  ')
  </code></pre>
  
  <p>So, in the bottom block I am trying to set environment variable (LATEST_BUILD) on the local machine after closing ssh, and also to paste there a value of variable (package_filename) which has been defined earlier during ssh session.</p>
  
  <p>The point here is that I see the last "Message sent to user" in the output, but the previous send "export LATEST_BUILD=12345\r" obviously does not work.</p>