Using exit
directly may be tricky as the script may be sourced from other places (e.g. from terminal). I prefer instead using subshell with set -e
(plus errors should go into cerr, not cout) :
set -e
ERRCODE=0
my_command || ERRCODE=$?
test $ERRCODE == 0 ||
(>&2 echo "My command failed ($ERRCODE)"; exit $ERRCODE)