I'm writing a script that logs into a remote server via SSH and executes some commands, one of which logs into a second remote server (which I can't access locally) via SSH and executes a command. After that nested SSH command, my original SSH command terminates and does't complete the rest of the heredoc. I've simplified the script a bit, but I get the same results:
#!/bin/bash
ssh server1 <<'EOF'
echo one $HOSTNAME
ssh server2 'echo two $HOSTNAME'
echo three $HOSTNAME
EOF
my output looks like this:
one server1
two server2
I would expect to see three server1
at the end of my output, but it doesn't happen. I'm able to separate these into two SSH commands and get what I need, but I'm curious why this is happening, and is it possible to get what I expect in one shot?