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

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


Try this statement:

exit 1

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


Use set -e

#!/bin/bashset -e/bin/command-that-fails/bin/command-that-fails2

The script will terminate after the first line that fails (returns nonzero exit code). In this case, command-that-fails2 will not run.

If you were to check the return status of every single command, your script would look like this:

#!/bin/bash# I'm assuming you're using makecd /project-dirmakeif [[ $? -ne 0 ]] ; then    exit 1ficd /project-dir2makeif [[ $? -ne 0 ]] ; then    exit 1fi

With set -e it would look like:

#!/bin/bashset -ecd /project-dirmakecd /project-dir2make

Any command that fails will cause the entire script to fail and return an exit status you can check with $?. If your script is very long or you're building a lot of stuff it's going to get pretty ugly if you add return status checks everywhere.


A SysOps guy once taught me the Three-Fingered Claw technique:

yell() { echo "$0: $*" >&2; }die() { yell "$*"; exit 111; }try() { "$@" || die "cannot $*"; }

These functions are *NIX OS and shell flavor-robust. Put them at the beginning of your script (bash or otherwise), try() your statement and code on.

Explanation

(based on flying sheep comment).

  • yell: print the script name and all arguments to stderr:
    • $0 is the path to the script ;
    • $* are all arguments.
    • >&2 means > redirect stdout to & pipe 2. pipe 1 would be stdout itself.
  • die does the same as yell, but exits with a non-0 exit status, which means “fail”.
  • try uses the || (boolean OR), which only evaluates the right side if the left one failed.