Bash Abort 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 about Stack Overflow the company Business Learn more about hiring developers or posting ads with bash abort script on error us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack bash abort trap 6 Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign bash abort function up Automatic exit from bash shell script on error up vote 276 down vote favorite 48 I've been writing some shell script and I would find it useful if there was the ability to halt the execution of said shell script if bash exit on error any of the commands failed. See below for an example: #!/bin/bash cd some_dir ./configure --some-flags make make install So in this case if the script can't change to the indicated directory then it would certainly not want to do a ./configure afterwards if it fails. Now I'm well aware that I could have an if check for each command (which I think is a hopeless solution), but is there a global setting to make the script exit if one of the commands fails? bash
Bash Exit On Error With Message
shell exit share|improve this question edited Mar 29 '15 at 23:26 asked May 20 '10 at 4:21 radman 5,57462242 I did have a quick look for duplicates and couldn't find any. –radman May 20 '10 at 4:22 1 answer goes to Adam for the detail regarding set -e (which is exactly wanted). Also thanks to a_m0d for the info on traps (though not 100% relevant). –radman May 20 '10 at 5:07 add a comment| 7 Answers 7 active oldest votes up vote 440 down vote accepted Use the set -e builtin: #!/bin/bash set -e # Any subsequent(*) commands which fail will cause the shell script to exit immediately Alternatively, you can pass -e on the command line: bash -e my_script.sh You can also disable this behavior with set +e. (*) Note: The shell does not exit if the command that fails is part of the command list immediately following a while or until keyword, part of the test in an if statement, part of a && or || list, or if the command's return value is being inverted via ! (from man bash) share|improve this answer edited Jan 19 '15 at 16:52 Gilead 91211322 answered May 20 '10 at 4:36 Adam Rosenfield 241k66369493 1 Is this also a Bourne Shell builtin? –Tom May 16 '12 at 19:03 3 @Tom Yes: See pubs.opengroup.org/onlinepubs/009695399/utilities/… –Max Nanasy Aug 3 '13 at 20:50 2 'Set +e' will revert the setting again, so you can have only ce
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 about Stack Overflow the company Business Learn more
Bash Exit On Error Code
about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges bash stop on error Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each bash set -e other. Join them; it only takes a minute: Sign up Aborting a shell script if any command returns a non-zero value? up vote 239 down vote favorite 71 I have a Bash shell script that invokes a number http://stackoverflow.com/questions/2870992/automatic-exit-from-bash-shell-script-on-error of commands. I would like to have the shell script automatically exit with a return value of 1 if any of the commands return a non-zero value. Is this possible without explicitly checking the result of each command? e.g. dosomething1 if [[ $? -ne 0 ]]; then exit 1 fi dosomething2 if [[ $? -ne 0 ]]; then exit 1 fi linux bash unix shell share|improve this question edited Sep 16 '12 at 11:01 Mat 135k20234273 asked May http://stackoverflow.com/questions/821396/aborting-a-shell-script-if-any-command-returns-a-non-zero-value 4 '09 at 18:55 Jin Kim 4,602113460 2 In addition to set -e, also do set -u (or set -eu). -u puts an end to the idiotic, bug-hiding behavior that you can access any nonexistent variable and have a blank value produced with no diagnostics. –Kaz Feb 21 '14 at 1:36 add a comment| 8 Answers 8 active oldest votes up vote 414 down vote accepted Add this to the beginning of the script: set -e This will cause the shell to exit immediately if a simple command exits with a nonzero exit value. A simple command is any command not part of an if, while, or until test, or part of an && or || list. See the bash(1) man page on the "set" internal command for more details. I personally start almost all shell scripts with "set -e". It's really annoying to have a script stubbornly continue when something fails in the middle and breaks assumptions for the rest of the script. share|improve this answer edited May 4 '09 at 19:16 answered May 4 '09 at 19:00 Ville Laurikari 16.8k54449 30 That would work, but I like to use "#!/usr/bin/env bash" because I frequently run bash from somewhere other than /bin. And "#!/usr/bin/env bash -e" doesn't work. Besides, it's nice to have a place to modify to read "set -xe" when I want to turn o
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 about Stack Overflow the company Business Learn more about http://unix.stackexchange.com/questions/52063/how-to-write-an-abort-on-error-script-without-adding-exit-to-every-line hiring developers or posting ads with us Unix & Linux Questions Tags Users Badges Unanswered Ask Question _ Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute: Sign up Here's how it works: Anybody can ask a question Anybody can answer The best answers are voted up and rise to the top How to write an abort-on-error script without adding `|| exit on error $?` to every line? up vote 4 down vote favorite 2 I'm writing a bash script that should exit the error code of the last failed command and not continue execution. This can be achieved by adding a || exit $? everywhere, but is there an easier way, e.g. a set option at the start to do this without uglifying every line? bash exit share|improve this question asked Oct 17 '12 at 10:02 Tobias Kienzler 3,30243469 1 A side bash exit on not is that exit $? is not needed. By default, your script will exit with the $? of the last command. exit $? and exit are equivalent in bash. –jordanm Oct 17 '12 at 17:55 @jordanm You mean the $? was superfluous? The exit itself (without set -e as I learned) was necessary. But thanks, good to know for situations where I don't want to abort on all errors. –Tobias Kienzler Oct 17 '12 at 17:58 1 Yes, I could have been more clear. The $? is superfluous. –jordanm Oct 17 '12 at 19:25 add a comment| 3 Answers 3 active oldest votes up vote 10 down vote accepted set -e ? set: set [-abefhkmnptuvxBCHP] [-o option-name] [--] [arg ...] Set or unset values of shell options and positional parameters. Change the value of shell attributes and positional parameters, or display the names and values of shell variables. Options: -a Mark variables which are modified or created for export. -b Notify of job termination immediately. -e Exit immediately if a command exits with a non-zero status. ... share|improve this answer edited Oct 17 '12 at 10:39 answered Oct 17 '12 at 10:09 Chris Card 1,606176 2 Instead of the dummy padding you could quote the documentation. –manatwork Oct 17 '12 at 10:09 there you go :) –Chris Card Oct 17 '12 at 10:39 1 Note that non-zero return codes only cause a s