ssh remotecluster \'bash -s\' << EOF
> export TEST=\"sdfsd\"
> echo $TEST
> EOF
This prints nothing.
Also it still does not
The variable assignment TEST="sdfsd"
given in the here document is no real variable assignment, i. e. the variable assignment will actually not be performed directly in the declaration / definition of the here document (but later when the here document gets evaluated by a shell).
In addition, the $TEST
variable contained in an unescaped or unquoted here document will be expanded by the local shell before the local shell executes the ssh
command.
The result is that $TEST
will get resolved to the empty string if it is not defined in the local shell before the ssh
command or the here document respectively.
As a result, the variable assignment export TEST="sdfsd"
in the here document will not take effect in the local shell, but first be sent to the shell of the remote host and only there be expanded, hence your prints nothing experience.
The solution is to use an escaped or single-quoted here document, <<\EOF
or <<'EOF'
; or only escape the \$TEST
variable in the here document; or just define the $TEST
variable before the ssh
command (and here document).
# prints sdfsd
export TEST="sdfsd"
ssh localhost 'bash -s' << EOF
echo $TEST
EOF
# prints sdfsd
ssh localhost 'bash -s' << EOF
export TEST="sdfsd"
echo \$TEST
EOF
# prints sdfsd
ssh localhost 'bash -s' <<'EOF'
export TEST="sdfsd"
echo $TEST
EOF
export variable, ssh send exported(environment) variables to server export VAR=test ssh -v 127.0.0.1 echo $VAR test above commands and see result