How To Exit A Bash Script On Error
Contents |
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more exit bash shell about Stack Overflow the company Business Learn more about hiring developers or posting ads
Bash Exit Function
with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack bash if exit code Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up In a bash script, how can I exit the entire script if
Unix Exit Codes
a certain condition occurs? up vote 239 down vote favorite 51 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 shell script exit a dun dun dun goto? bash scripting share|improve this question asked Sep 4 '09 at 9:51 samoz 20.8k39109168 add a comment| 6 Answers 6 active oldest votes up vote 226 down vote accepted Try this statement: exit 1 Replace 1 with appropriate error codes. See also Exit Codes With Special Meanings. share|improve this answer edited Aug 7 '15 at 7:10 flying sheep 2,96722245 answered Sep 4 '09 at 9:53 Michael Foukarakis 20.6k35090 42 0 isn't good status to exit with if an error happens. –Michał Górny Sep 4 '09 at 9:59 4 You're right, edited for clarity. –Michael Foukarakis Sep 4 '09 at 10:00 @MichałGórny what would be a good status code? –CMCDragonkai May 14 '14 at 2:39 3 @CMCDragonkai, usually any non-zero code will work. If you don't need anything special, you can just use 1 consistently. If the script is meant to be run by another script, you may want to define your own set of status code with particular meaning. For example, 1 == tests failed, 2 == compilation failed. If the script is part of something else, you may need to adjust the codes to match the practices used there. For example, when part of
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn
Linux Exit Code
more about Stack Overflow the company Business Learn more about hiring developers or posting
Bash Exit Script From Function
ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack bash return Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up Stop on first error [duplicate] up vote 121 down vote favorite http://stackoverflow.com/questions/1378274/in-a-bash-script-how-can-i-exit-the-entire-script-if-a-certain-condition-occurs 28 Possible Duplicate: Automatic exit from bash shell script on error How can I have bash stop on the first command failure, without putting stuff like this all through my code? some_prog || exit 1 some_other_prog || exit 1 bash share|improve this question asked Aug 13 '10 at 6:45 Matt Joiner 41.6k44216383 marked as duplicate by martin clayton, Barmar, Toto, codesparkle, Emil Vikström Oct 13 '12 at 10:31 http://stackoverflow.com/questions/3474526/stop-on-first-error This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question. add a comment| 1 Answer 1 active oldest votes up vote 241 down vote accepted Maybe you want set -e: http://www.davidpashley.com/articles/writing-robust-shell-scripts.html#id2382181 share|improve this answer answered Aug 13 '10 at 6:50 Alok Singhal 49.2k1291126 32 Be aware of set -e gotchas: mywiki.wooledge.org/BashFAQ/105 –Kris Jun 9 '15 at 10:57 1 @Kris thanks for the link, very interesting. –Alok Singhal Jun 10 '15 at 3:45 2 @Kris, you just broke my heart. I thought -e was infallible. –Trenton Sep 30 '15 at 3:17 add a comment| Not the answer you're looking for? Browse other questions tagged bash or ask your own question. asked 6 years ago viewed 70815 times active 6 years ago Linked 282 Automatic exit from bash shell script on error 196 What does set -e mean in a bash script? 62 How to have GNU make explicitly test for failure? 12 Execute multiple commands in a bash script sequentially and fail if at least one of them fails 1 Bash play sound on first error 0 Stop bash script when find gets to a folder with
Bash Prompts About Writing Robust Bash Shell Scripts Many people hack together shell scripts quickly to do simple tasks, but these soon take on a life of their own. Unfortunately shell scripts are full of subtle effects which result in scripts failing in http://www.davidpashley.com/articles/writing-robust-shell-scripts/ unusual ways. It's possible to write scripts which minimise these problems. In this article, I explain several techniques for writing robust bash scripts. Use set -u How often have you written a script that broke because a variable wasn't set? I know I have, many times. chroot=$1 ... rm -rf $chroot/usr/share/doc If you ran the script above and accidentally forgot to give a parameter, you would have just deleted all of your system exit code documentation rather than making a smaller chroot. So what can you do about it? Fortunately bash provides you with set -u, which will exit your script if you try to use an uninitialised variable. You can also use the slightly more readable set -o nounset. david% bash /tmp/shrink-chroot.sh $chroot= david% bash -u /tmp/shrink-chroot.sh /tmp/shrink-chroot.sh: line 3: $1: unbound variable david% Use set -e Every script you write should include set -e at the top. This tells bash how to exit that it should exit the script if any statement returns a non-true return value. The benefit of using -e is that it prevents errors snowballing into serious issues when they could have been caught earlier. Again, for readability you may want to use set -o errexit. Using -e gives you error checking for free. If you forget to check something, bash will do it or you. Unfortunately it means you can't check $? as bash will never get to the checking code if it isn't zero. There are other constructs you could use: command if [ "$?"-ne 0]; then echo "command failed"; exit 1; fi could be replaced with command || { echo "command failed"; exit 1; } or if ! command; then echo "command failed"; exit 1; fi What if you have a command that returns non-zero or you are not interested in its return value? You can use command || true, or if you have a longer section of code, you can turn off the error checking, but I recommend you use this sparingly. set +e command1 command2 set -e On a slightly related note, by default bash takes the error status of the last item in a pipeline, which may not be what you want. For example, false | true will be considered to have succeeded. If you wou