How do I return a failure value from a bash function?
Solution 1:
Add
return $?
below the cmake
call to exit the function with the same exit code as cmake
, and add
return 1
below the echo
call to exit with 1
, which is non-zero and therefore indicates an error.
You can alternatively (if you call both commands in combination) add the make
call to this function:
cmake [args] && make -j4
Or, to allow e.g. for more fine-grained error handling:
cmake [args]
local _ret=$?
if [ $_ret -ne 0 ] ; then
echo "cmake returned with exit code $_ret, aborting!" >&2
return $_ret
fi
make -j4
If you call it as a standalone program, i.e. store the function in a shell script, and invoke e.g. as
/path/to/cmakerel.sh
then you can use exit
instead of return
to abort the whole program/subshell.