Search code examples
bashexit-code

How to make a bash function return 1 on any error


I have a fake bash function such as this:

has_error() {
  true
  echo "Ok..."
  false
  echo "Shouldn't be here!"
}

What I'd like to have happen when I run this function and check the error status:

> has_error; echo $?
Ok...
1

But what actually happens:

> has_error; echo $?
Ok...
Shouldn't be here!
0

The problem is that the function keeps executing after an error has been thrown, and moreover, I can't even detect that an error was thrown. How can I change this? I could put a set -e at the beginning of the function, but then my whole shell will terminate if an error gets thrown. What I'd like is to simply have it return and set the exit status to 1. I could do it with putting && in between the commands, or by suffixing every line with || return 1, but neither of those are very elegant. What's the proper way to do this?

EDIT:

It seems that I'm not being clear enough, because a lot of the responses seem to suggest that I don't actually know how to perform tests in bash. As I mention above, I am aware that I could manually test each command in my function, and return or handle errors as I wanted. I'm also aware that I can set -e and cause my shell session to terminate on an error. But what I'm asking is: is there a way to have a function cease to continue execution -- without an explicit test -- if any of the commands within that function return nonzero statuses?


Solution

  • If your function doesn't need to set any global variables, you can have the function create a subshell in which set -e is used. The subshell will exit, not the shell in which has_error is called.

    has_error () (    # "(", not "{"
      set -e
      true
      echo "Ok..."
      false
      echo "Shouldn't be here!"
    )