How can i get variable value inside << EOS?

Hi,

Below is my code:

user="wluser"
 password="welcom123"
 ip=$1
  
 expect << 'EOS'

spawn sftp  ${user}@${ip}:/
expect "Password:"
send "${pass}\n"
expect "sftp>"
send "get *date.csv\n"
expect "sftp>"
send "bye\n"
EOS

I am not able to get the value of user, pass and ip variables inside the << 'EOS'

Can you please suggest how can I get the values of them inside << EOS spawn sftp ${user}@${ip}:/
?

try loosing single quote in 'EOS' -> EOS

1 Like

You are refering to a Tcl variable user, which does not exist.

Then, you are using a (non-exported) shell variable user. Since it is not exported, no subprocess could see it.

Solution: Define an environment variable (which you can do by exporting a shell variable).

You didn't specify which shell you are using, so I'm assuming Posix shell:

EXPECT_USER=wluser
export EXPECT_USER

In your Tcl script, you can then access it as $::env(EXPECT_USER)

Note that it is strongly recommended to use all-uppercase for environment variables.

@rovf, passing environment variables is an interesting alternative, but is not needed here because the expect script is prepared in the shell.
In detail, the shell substitutes the shell variables by their values, writes the result to the here document, and then passes it to the expect program as stdin.

Good point. However, this approach is dangerous, Imagine the effect if someone decides to change the password to

[error HaHaHa]

or some even nastier string. I would really avoid shell interpolation into a Tcl program unless I can be absolutely sure that the interpolated code does not ruin by Tcl code.