In short, use:
- unquoted heredoc keywords, e.g.,
- regular dollar char for outer (i.e. local) variables, e.g.,
- escaped dollar char for inner (i.e. remote) variables, e.g.
If you leave the heredoc keyword (i.e.
EOF) unquoted then the heredoc body is processed locally, so that
$FOO is expanded to
BAR is expanded to the empty string. Then your
ssh command becomes:
If you quote the heredoc keyword then variable expansion is suppressed, so that your
ssh command becomes this instead:
FOO is probably not defined in the remote shell environment, the expression
"FOO=$FOO" is evaluated as
FOO is set to the empty string.
If you want to use both variables then you'll need to leave the heredoc keyword unquoted, so that variable expansion takes place for the locally defined variable, and then escape (with a backslash) the variable that you want to be expanded remotely, i.e.:
ssh some.remote.host << EOF
In this case your ssh command (as received by the remote server) will be the following: