Strange behaviour with "set -e" and functions

Hi,

This seems to be a recurrent problem on mailing lists and bug reports, but I've been unable to find a solution. Let's imagine we have this bash script:

#!/bin/bash
set -e

fun() {
  echo "fun_start"
  test 1 = 2 
  echo "fun_end"
}

echo "main_start"
fun 
echo "main_end"

This outputs:

main_start
fun_start

Right, but now I want the script to continue if "fun" fails, so I protect it with a "||":

echo "main_start"
fun || echo "fun failed"
echo "main_end"

And that's what I get (!):

main_start
fun_start
fun_end
main_end

The function continues even though there has been an error and my conditional statement is not even run. It seems that "set -e" is being ignored inside the function. That seems very counterintuitive. The only solution I found was adding "|| return 1" to every "dangerous" call inside a function, but then what's the point of using set -e?

So, am I doing something wrong? is there any easy way to get this desired output?

main_start
fun_start
fun failed
main_end

thanks

Bash doc, set

This is generic for every posix-shells:

fun() {
  echo "fun_start"
  test 1 = 2  || return 1
  echo "fun_end"
}

echo "main_start"
fun || echo "fun failed"
echo "main_end"