1

I am trying to paas an env variable to a remoye host using the code below.

ssh username@ip "env current_quarter=$current_quarter" sh -s << 'EOF' 
pwd 
echo "${current_quarter}" 
EOF

pwd prints the correct location on the remote host but echo "${current_quarter}" does not print the current_quarter.

sshtest
  • 13

1 Answers1

2

By putting EOF in quotes you are telling the local shell to not expand the here document before passing it to ssh. According to the bash documentation this applies to parameter expansion, command substitution and arithmetic expansion.

Contrary to what I thought when originally writing this answer the remote shell is not affected through this and will execute the here document like you wrote it directly in the remote shell (including all the kinds of expansion and substitution that were skipped in the local shell by quoting EOF).

If you just want to use the local variable test you remove the quotes from the EOF:

ssh user@host sh -s <<EOF
pwd
echo $test
EOF

For fish users it would look like this:

ssh user@host "\
pwd
echo $test
"

Now if you want to pass the local variable test as the remote variable remote_test and not expand locally you just escape the variable with a backslash:

ssh user@host sh -s <<EOF
export remote_test=$test
pwd
echo "\${remote_test}"
EOF

Edit:

Running:

ssh user@host "env remote_test=$test" sh -s <<'EOF'
pwd
echo "${remote_test}"
EOF

should be equivalent to the third code example with the exception that env is not part of the shell and can also be used in non-bourne shells.

(This is however identical to the code example OP gave but since OP marked this answer as correct I suspect that env and export behave differently when encountering certain character sequences but I have not found conclusive evidence supporting this hypothesis.)