As a Linux Systems Administrator I do quite a bit of scripting, and oftentimes I’m doing much of this in bash especially when using Jenkins or Hudson to give a nice interface and accessibility to build and deployment for the shop I’m working with. Often times, you want these jobs to fail if there’s a problem with any step along the way, but you’re doing steps where you need to ssh to other servers and run commands, or something similar.

In a script, ssh’s exit code will be that of the last executed command. Executed is the key word there; there are times when it will continue to run other commands as well. I wrote a quick gist to illustrate the point.

https://gist.github.com/asiegman/5256589

The main thing to remember here is to use the right combination of multiple-line ssh formatting and environment settings. Also to remember, the script you’re running it from will not pick up any errors from inside the SSH connection if you’re using the “set -e” option, since the only command actually run from that script’s shell is the SSH command.

Hope that helps!