Jugi Jugi - 10 months ago 57
Bash Question

How to exit from shell script if any of the command in SSH returns non-zero value

I have a shell script with below ssh command to perform few actions,

$SSH $user@$remoteIpAddress "sudo rm -rf $remoteLocation/xxx/*; cd $remoteLocation/yyy; .... ... "

Directory $remoteLocation/yyy is not available and it will display the error as,

./test.sh: line 6: cd: /opt/test/yyy: No such file or directory

and it is proceeding to the next line. My scenario is to exit from the shell script itself if any command in ssh returns non-zero value. I could add set -e to exit from shell but i am not sure how to handle it in ssh. Thanks in advance.


I have few lines of command below the SSH command. I need a solution to exit entirely from the script and not to execute any lines below the ssh command.

Answer Source

Keep in mind that your set -e idea applies just as well to the shell you open on the other end with ssh. So you can prepend your ssh commands with that to ensure an error terminates the ssh session:

$SSH $user@$remoteIpAddress "set -e; sudo rm -rf $remoteLocation/xxx/*; cd $remoteLocation/yyy; .... ... "

Then the ssh shell will exit with non-zero error code as soon as an error is encountered, and your script will exit as well if you have set -e earlier in your script.