In a Bash script, how can I exit the entire script if a certain condition occurs?

samoz picture samoz · Sep 4, 2009 · Viewed 722.5k times · Source

I'm writing a script in Bash to test some code. However, it seems silly to run the tests if compiling the code fails in the first place, in which case I'll just abort the tests.

Is there a way I can do this without wrapping the entire script inside of a while loop and using breaks? Something like a dun dun dun goto?

Answer

Michael Foukarakis picture Michael Foukarakis · Sep 4, 2009

Try this statement:

exit 1

Replace 1 with appropriate error codes. See also Exit Codes With Special Meanings.