-1

(This is RHEL 8.4, if it matters.)

The echo commands on lines 5 and 7 in this script run on the local host, but the hostname commands on lines 6 and 8 run on the remote host as expected.

Why do lines 5 and 7 run on the local host? More importantly, how do I get them to run on the remote host?

EDIT: I do not want to pass in variables. I want the variables to be solely defined and used on the remote server.

 1 #!/bin/bash
 2 export foo=bar
 3 ssh -q ${1} <<EOF
 4     export foo=blarge
 5     echo $foo
 6     hostname
 7     echo $foo
 8     hostname
 9 EOF
10 hostname
RonJohn
  • 1,148
  • @ilkkachu NO because I do not want to pass variables, and do not think that my question indicates such a desire. If it does, please indicate how. – RonJohn Dec 06 '21 at 03:07
  • 2
    did you even read the answer there? "If you leave the heredoc keyword (i.e. EOF) unquoted then the heredoc body is processed locally, so that $FOO is expanded... " and "If you quote the heredoc keyword then variable expansion is suppressed – ilkkachu Dec 06 '21 at 09:22

1 Answers1

2

Use quoted EOF:

$ ssh -q localhost <<'EOF'
export foo=blarge
echo $foo
EOF
blarge

You didn't specify what shell you use but in man bash for example it says:

If any part of word is quoted, the delimiter is the result of quote removal on word, and the lines in the here-document are not expanded.