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?
Use the
set -ebuiltin:Alternatively, you can pass
-eon the command line:You can also disable this behavior with
set +e.You may also want to employ all or some of the the
-e-u-xand-o pipefailoptions like so:-eexits on error,-uerrors on undefined variables,-xprints commands before execution, and-o (for option) pipefailexits on command pipe failures. Some gotchas and workarounds are documented well here.(*) Note:
(from
man bash)