How do I return a failure value from a bash function?
--------------------------------------------------
Hire the world's top talent on demand or became one of them at Toptal: https://topt.al/25cXVn
and get $2,000 discount on your first invoice
--------------------------------------------------
Take control of your privacy with Proton's trusted, Swiss-based, secure services.
Choose what you need and safeguard your digital life:
Mail: https://go.getproton.me/SH1CU
VPN: https://go.getproton.me/SH1DI
Password Manager: https://go.getproton.me/SH1DJ
Drive: https://go.getproton.me/SH1CT
Music by Eric Matyas
https://www.soundimage.org
Track title: Over Ancient Waters Looping
--
Chapters
00:00 How Do I Return A Failure Value From A Bash Function?
00:39 Accepted Answer Score 14
01:24 Thank you
--
Full question
https://superuser.com/questions/371536/h...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#bash
#avk47
Hire the world's top talent on demand or became one of them at Toptal: https://topt.al/25cXVn
and get $2,000 discount on your first invoice
--------------------------------------------------
Take control of your privacy with Proton's trusted, Swiss-based, secure services.
Choose what you need and safeguard your digital life:
Mail: https://go.getproton.me/SH1CU
VPN: https://go.getproton.me/SH1DI
Password Manager: https://go.getproton.me/SH1DJ
Drive: https://go.getproton.me/SH1CT
Music by Eric Matyas
https://www.soundimage.org
Track title: Over Ancient Waters Looping
--
Chapters
00:00 How Do I Return A Failure Value From A Bash Function?
00:39 Accepted Answer Score 14
01:24 Thank you
--
Full question
https://superuser.com/questions/371536/h...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#bash
#avk47
ACCEPTED ANSWER
Score 14
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.