Check if an command was successful in bash

When scripting gigantic build scripts with bash, its important that if anything fails you halt the script.

Heres how:

function checkResult() {
	RESULT=$1
	if [ $RESULT -ne 0 ] ; then
		echo
		echo "!!!!!!!!!!!  Build Failed !!!!!!!!!!!!!!!!!"
		echo
		exit $RESULT
	fi	
}

You call it like this:

./someCommandOrScript.sh
checkResult $?
Share