0

The result of the following bash script is "empty line" in output

ssh dev@my_service <<EOF
  START_SERVICE_TIME_IS_UP=124
  echo $START_SERVICE_TIME_IS_UP
  exit  
EOF

Can't understand why. I'd expect to see "124" in output. The remote server is running Red Hat linux.

jopasserat
  • 5,721
  • 4
  • 31
  • 50
Sb Lon
  • 111
  • 2
  • 6
  • 1
    You could have used [shellcheck](http://www.shellcheck.net). It says "[Quote 'EOF'](https://github.com/koalaman/shellcheck/wiki/SC2087) to make here document expansions happen on the server side rather than on the client." – that other guy Jan 02 '16 at 18:08

2 Answers2

8

Variables in a here-doc are expanded by the local shell, unless you quote the here-doc. Putting the end-token in quotes quotes the here-doc.

ssh dev@my_service <<'EOF'
  START_SERVICE_TIME_IS_UP=124
  echo $START_SERVICE_TIME_IS_UP
  exit  
EOF
Barmar
  • 741,623
  • 53
  • 500
  • 612
-3

always use "" echo "$START_SERVICE_TIME_IS_UP" something handy to use echo https://github.com/united-bashers/mkbash

Thhollev
  • 73
  • 1
  • 1
  • 10
  • (1) The quotes don't fix the problem here: The value is being expanded when the heredoc is written, *before* the variable is defined. (2) Please be explicit about how links relate to the question you're answering -- otherwise they look like spam. – Charles Duffy Apr 20 '18 at 19:53