0

I'm trying to ssh into a remote host, load an environment variable in the .zshrc file on the host, then execute a command using that environment variable.

Here's my script so far,

#!/usr/bin/expect -f
spawn ssh hostname
send "echo $my_variable\n"
interact

and the .zshrc file on my remote host has these contents,

export my_variable=banana_tree

I get this output when running my script above,

spawn ssh hostname
can't read "my_variable": no such variable
    while executing
"send "echo $my_variable\n""
    (file "/Users/me/myscript.sh" line 6)

I know the error message is saying expect can't find the variable because it's not defined in the environment where I'm executing the script, and one workaround is to define the variable where I'm running the script, however I'd prefer to keep the variable defined on the remote host only if possible.

1 Answer 1

0

I had to escape the dollar sign in my script, so it became,

#!/usr/bin/expect -f
spawn ssh hostname
send "echo \$my_variable\n"
interact

It looks like .zshrc was being loaded, but to load the environmental variable on the host I had to escape the dollar sign, whereas if I want to define the environmental variable in my script I would not escape the dollar sign.

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.