how to exit from script if heredoc command is failing how to exit from script if heredoc command is failing shell shell

how to exit from script if heredoc command is failing


You can check the exit status of any critical command in your heredoc and exit the ssh with an error - then check the ssh exit status to exit your script

ssh host@IP <<ENDSSHls n1n || exit 1cd n1n || exit 2ENDSSH(($?)) && exit 1echo 'done'


You can instruct the shell to exit if anything errors like in any shell script:

$ sudo su <<EOF              set -eecho beforebad-commandecho afterEOF

Output:

beforesh: line 3: bad-command: command not found

Notice after isn't printed.


The commands in the here decument are executed by a remote shell. You need to pass in set -o errexit inside the here document in order for it to be visible there; it is entirely separate from the shell which runs the ssh command.