How to stop the bash script when a condition fails?

Here it shows to use the || and && in a single line to concatenate the execution of commands: How can I check for apt-get errors in a bash script?

I am trying to stop a script execution if a certain condition fails,

e.g.

false || echo "Obvious error because its false on left" && exit

Here it prints Obvious error because its false on the left and exits the console which is what I wanted.

true || echo "This shouldn't print" && exit

Here, there's no echo print but the exit command runs as well, i.e., the console is closed, shouldn't the exit command not run because the echo command on the right was not executed? Or is by default a statement considered false on the left hand side of an && operator?

Edit: I should have mentioned it before, my aim was to echo the error and exit if it was not clear. w.r.t to my specific case of catching errors when grouping conditions using && and ||, @bodhi.zazen answer solves the problem.

@takatakatek answer makes it more clear about the flow control and the bash guide links are excellent

@muru answer has good explanation of why not to use set -e if you want custom error messages to be thrown with alternatives of using perl and trap which I think is a more robust way and see myself using it from my second bash script onwards!


The problem is that || and && only skip one subsequent stanza of a command chain when the condition fails. If you write out a full block structure it makes perfect sense. What you wrote becomes this:

if ! true ; then
   echo "This shouldn't print"
else
   exit
fi

What you want is this:

if ! true ; then
   echo "This shouldn't print"
   exit
fi

When you are using Bash's shortcut conditional operators it is better to avoid mixing them. The logic is much easier to understand while you're writing it, and your readers will appreciate your good style.

  • http://mywiki.wooledge.org/BashGuide/TestsAndConditionals#Control_Operators
  • http://mywiki.wooledge.org/BashPitfalls#cmd1_.26.26_cmd2_.7C.7C_cmd3

You probably want (as pointed out by steeldriver)

true || { echo "This shouldn't print" && exit; }

Otherwise your script is reading one conditional at at time

a or b and then exit

I think you want a or (b and exit) ?


You could start the script with:

#!/bin/bash -e

The -e ensures that the script exits the moment something returns false (or any non-zero number). A specific failure can then be avoided with something || true

Alternatively, if you want to postpone error checking until later in the script, you can use set -e:

#!/bin/bash
false  # script continues happily
set -e
false  # script stops

The simplest way to fail on error is to use bash's -e option (set -e or /bin/bash -e in the shebang). However, this doesn't make it easy to send custom error messages. There are a couple of idioms that might make it simpler:

die à la Perl

Perl has a very convenient die command which prints a message to stderr and raises an exception, which usually leads to the script terminating. You can emulate this:

die () {
  ret=$?
  print "%s\n" "$@" >&2
  exit "$ret"
}

false || die "Obvious error because its false on left"
true || die "This shouldn't print"

trap ERR

The trap <command> command can be used to run a command when a signal is received, or when exiting the script (trap ... EXIT), or when a command returns an error (trap ... ERR). So something like:

trap 'ret=$?; printf "%s\n" "$ERR_MSG" >&2; exit "$ret"' ERR

Then:

ERR_MSG="Obvious error because its false here"
false
ERR_MSG="This shouldn't print"
true

In either case, I'd suggest using at least exit 1, to indicate that the script failed. A plain exit will use the exit status of the previous command, which in these cases would be the echo or printf commands, which would usually succeed. Saving the exit status of the failing command like I have done here would be a nice-to-have.