I have a fragment in a bash script:
ssh -oStrictHostKeyChecking=no -T $SSH_HOSTNAME -p $SSH_PORT <<EOM
set -e
echo "test"
exit
EOM
echo "test 2"
Statement "test" is displayed but "test 2" is not.
I also tried to use "exit 0" instead of "exit".
The script worked on an older version of debian (2016), but stopped working after the update (2021).
Why the script stopped working and how can I fix it?
2
Answers
Since this is only a fragment, do you have
set -e
enabled before the fragment in the script? If so, then I assume ssh is returning a non-zero exit code and due toset -e
being enabled the script exits before reachingecho "test 2"
I tested that and, indeed, sending the command with heredoc and this combination of
set -e
andexit
returns a 1 and not a 0, as tested.But I think that, somewhere, another
set -e
is set in the context whereecho "test2"
is executed.We have some solutions that would work:
exit
because it’s not needed, you’re sending commands to a target ssh host and the termination of session it’s automatic when the heredocs finish.ssh target "set -e ; echo "test"; exit"
works just fine(somehow), but you have to take care of string escaping.