809

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 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?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
radman
  • 17,675
  • 11
  • 42
  • 58

8 Answers8

1291

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.

You may also want to employ all or some of the the -e -u -x and -o pipefail options like so:

set -euxo pipefail

-e exits on error, -u errors on undefined variables, -x prints commands before execution, and -o (for option) pipefail exits on command pipe failures. Some gotchas and workarounds are documented well here.

(*) 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 following the if or elif reserved words, part of any command executed in a && or || list except the command following the final && or ||, any command in a pipeline but the last, or if the command's return value is being inverted with !

(from man bash)

ADTC
  • 8,999
  • 5
  • 68
  • 93
Adam Rosenfield
  • 390,455
  • 97
  • 512
  • 589
  • 7
    Is this also a Bourne Shell builtin? – Tom May 16 '12 at 19:03
  • 6
    @Tom Yes: See http://pubs.opengroup.org/onlinepubs/009695399/utilities/set.html#tag_04_127_03 – Max Nanasy Aug 03 '13 at 20:50
  • 11
    'Set +e' will revert the setting again, so you can have only certain blocks that exit automatically on errors. – olivervbk Apr 18 '15 at 14:36
  • 3
    If shell script executes commands on a remote server will it also break if any of remote commands will produce an error or does `set -e` have also be included into remote commands sequence? – user487772 May 30 '15 at 10:30
  • 1
    Also, issue `set -o pipefail` if you want to catch failed commands in pipes such as `command1 | command2` (where `command1` failed): *The exit status of a pipeline is the exit status of the last command in the pipeline, unless the pipefail option is enabled (see The Set Builtin). If pipefail is enabled, the pipeline’s return status is the value of the last (rightmost) command to exit with a non-zero status, or zero if all commands exit successfully.* – Ohad Schneider Mar 30 '16 at 08:42
  • 2
    Is there a possibility to exit when a command in `$(...)` fails? If I use `set -e` and write `false`, the shell exits. But it does not exit on `echo $(false)`. Is there a way to *fix* this? – JojOatXGME Apr 03 '16 at 18:49
  • Does this print out the command line that failed if it failed? – Jonny Apr 12 '16 at 05:44
  • @Jonny, if you use `set -o verbose` or `set -x` the commands issued will be "echoed" similar to DOS bat files without "echo off". Not only the last one, though. – MattBianco Apr 26 '17 at 16:37
  • Similarly, you can use `unset -e` to turn it off if the context you're running your script in has it enabled already. – z0r Jun 01 '17 at 09:34
  • 5
    Please be aware that `set -e` is a shaky feature that might sometimes just not do what you expect. See here for a wonderful explanation: http://mywiki.wooledge.org/BashFAQ/105 – jlh May 25 '18 at 08:22
  • Does it work on the shebang line? – William Entriken Oct 23 '18 at 01:28
  • Great answer, would be nice to also add explanation for `-x` as you used it in your example.`-x Print commands and their arguments as they are executed.` from [man page](http://linuxcommand.org/lc3_man_pages/seth.html) – makozaki Aug 11 '20 at 11:10
  • `-x` enables a mode of the shell where all executed commands are printed to the terminal – I.G. Pascual Dec 05 '20 at 15:40
  • `set` is documented on line 3229 in `man bash`, at least on Ubuntu. You can search for it by typing `/` to search, and then entering `set [` There are a mind boggling amount of options that are very useful for controlling how your shell, or scripts, run. – Life5ign Jul 06 '22 at 17:37
84

To exit the script as soon as one of the commands failed, add this at the beginning:

set -e

This causes the script to exit immediately when some command that is not part of some test (like in a if [ ... ] condition or a && construct) exits with a non-zero exit code.

sth
  • 222,467
  • 53
  • 283
  • 367
  • More information here: http://www.davidpashley.com/articles/writing-robust-shell-scripts.html#id2382181 – dhornbein Jun 13 '13 at 03:39
76

Use it in conjunction with pipefail.

set -e
set -o pipefail

-e (errexit): Abort the script at the first error, when a command exits with non-zero status (except in until or while loops, if-tests, and list constructs)

-o pipefail: Causes a pipeline to return the exit status of the last command in the pipe that returned a non-zero return value.

Chapter 33. Options

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Mike
  • 20,010
  • 25
  • 97
  • 140
59

Here is how to do it:

#!/bin/sh

abort()
{
    echo >&2 '
***************
*** ABORTED ***
***************
'
    echo "An error occurred. Exiting..." >&2
    exit 1
}

trap 'abort' 0

set -e

# Add your script below....
# If an error occurs, the abort() function will be called.
#----------------------------------------------------------
# ===> Your script goes here
# Done!
trap : 0

echo >&2 '
************
*** DONE *** 
************
'
supercobra
  • 15,810
  • 9
  • 45
  • 51
30

An alternative to the accepted answer that fits in the first line:

#!/bin/bash -e

cd some_dir  

./configure --some-flags  

make  

make install
Petr Peller
  • 8,581
  • 10
  • 49
  • 66
  • 10
    I've seen this before (putting '-e' in the #! line), but for me it does not work as expected with bash v3.2.57 on Mac OS X. Simple test scripts that invoke /usr/bin/false followed by echo do not bail when expected. Using "set -e" as accepted above works fine. – pauldoo Sep 02 '15 at 19:45
23

One idiom is:

cd some_dir && ./configure --some-flags && make && make install

I realize that can get long, but for larger scripts you could break it into logical functions.

Matthew Flaschen
  • 278,309
  • 50
  • 514
  • 539
19

I think that what you are looking for is the trap command:

trap command signal [signal ...]

For more information, see this page.

Another option is to use the set -e command at the top of your script - it will make the script exit if any program / command returns a non true value.

CoolOppo
  • 503
  • 9
  • 17
a_m0d
  • 12,034
  • 15
  • 57
  • 79
13

One point missed in the existing answers is show how to inherit the error traps. The bash shell provides one such option for that using set

-E

If set, any trap on ERR is inherited by shell functions, command substitutions, and commands executed in a subshell environment. The ERR trap is normally not inherited in such cases.


Adam Rosenfield's answer recommendation to use set -e is right in certain cases but it has its own potential pitfalls. See GreyCat's BashFAQ - 105 - Why doesn't set -e (or set -o errexit, or trap ERR) do what I expected?

According to the manual, set -e exits

if a simple commandexits with a non-zero status. 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 a if statement, part of an && or || list except the command following the final && or ||, any command in a pipeline but the last, or if the command's return value is being inverted via !".

which means, set -e does not work under the following simple cases (detailed explanations can be found on the wiki)

  1. Using the arithmetic operator let or $((..)) ( bash 4.1 onwards) to increment a variable value as

    #!/usr/bin/env bash
    set -e
    i=0
    let i++                   # or ((i++)) on bash 4.1 or later
    echo "i is $i" 
    
  2. If the offending command is not part of the last command executed via && or ||. For e.g. the below trap wouldn't fire when its expected to

    #!/usr/bin/env bash
    set -e
    test -d nosuchdir && echo no dir
    echo survived
    
  3. When used incorrectly in an if statement as, the exit code of the if statement is the exit code of the last executed command. In the example below the last executed command was echo which wouldn't fire the trap, even though the test -d failed

    #!/usr/bin/env bash
    set -e
    f() { if test -d nosuchdir; then echo no dir; fi; }
    f 
    echo survived
    
  4. When used with command-substitution, they are ignored, unless inherit_errexit is set with bash 4.4

    #!/usr/bin/env bash
    set -e
    foo=$(expr 1-1; true)
    echo survived
    
  5. when you use commands that look like assignments but aren't, such as export, declare, typeset or local. Here the function call to f will not exit as local has swept the error code that was set previously.

    set -e
    f() { local var=$(somecommand that fails); }        
    g() { local var; var=$(somecommand that fails); }
    
  6. When used in a pipeline, and the offending command is not part of the last command. For e.g. the below command would still go through. One options is to enable pipefail by returning the exit code of the first failed process:

    set -e
    somecommand that fails | cat -
    echo survived
    

The ideal recommendation is to not use set -e and implement an own version of error checking instead. More information on implementing custom error handling on one of my answers to Raise error in a Bash script

Community
  • 1
  • 1
Inian
  • 80,270
  • 14
  • 142
  • 161