My shell script is run over SSH this way:

ssh -i mycert.pem testuser@myserver.com <<SSHBLOCK
pm2 logs --lines 100 &
sleep 3
kill $pid

The script works as expected when on local server (not wrapped in sshblock). But when I need to get logs on remote server, $pid is just blank. Why is $! empty when used in the SSHBLOCK above?

  • 1
    Hint: Where is $! being expanded? In the local shell, where the heredoc is created, or the remote shell, where the heredoc contents are executed? stackoverflow.com/questions/4937792/… – muru Jul 10 '17 at 8:19

You must use a quote '' or escape \ to expand the variable remotely.So,use the here document <<'SSHBLOCK' OR <<\SSHBLOCK

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.