I prefer to write solid shell code, so the errexit & nounset is alway set.
The following code will stop at bad_command line
#!/bin/b
How about this? If you want the actual exit code ...
#!/bin/sh set -e cat /tmp/doesnotexist && rc=$? || rc=$? echo exitcode: $rc cat /dev/null && rc=$? || rc=$? echo exitcode: $rc
Output:
cat: /tmp/doesnotexist: No such file or directory exitcode: 1 exitcode: 0