So I have an RSA token set in my environment variable. I want to set it in the environment of the remote machine. I tried this below script to set, but it only sets first line -----BEGIN OPENSSH PRIVATE KEY-----:

#!/usr/bin/expect -f

spawn ssh -tt ec2-user@$::env(EC2_INSTANCE)

send "export RSA_KEY=$::env(MY_KEY)\r"
expect -re ".*\$"

How can I set the whole content?


1 Answer 1


Put its value in single quotes:

send "export RSA_KEY='$::env(MY_KEY)'\r"
expect -re {.*\$}

Notice the {.*\$} instead of ".*\$"; Your ".*\$" will turn into .*$ and match anything, including the empty string.

If the MY_KEY environment variable can contain 's, you may want to escape them:

send "export RSA_KEY='[regsub -all {'} $::env(MY_KEY) {'\''}]'\r"
expect -re {.*\$}
  • Thanks! It worked.
    – was_777
    Mar 2, 2020 at 5:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.