title: Trap not activated when calling functions
body: <p>I am using set -e and a trap handler to produce error messages is my ksh scripts.</p>
  
  <pre><code>#!/bin/ksh
  set -e
  
  myexit()
  {
      if [[ $1 != 0 ]]; then
           echo "ERROR: Script $0 failed unexpectedly with signal $1!"    
      fi
  }
  
  settrap()
  {
      for sig in INT TERM EXIT; do
          #echo "setting trap for $sig..."
          trap "code=$?;trap - INT TERM EXIT;myexit $code \"$sig\"; [[ $sig == EXIT ]] || kill -$sig $$" $sig
      done
  } 
  settrap
  </code></pre>
  
  <p>Now I have the strange behavior that this works for calling old-style functions, but not for functions calling functions.</p>
  
  <pre><code>test1()
  {
      echo "test1"
      eval test2
  }
  
  test2()
  {
      echo "test2"
      return -1
  }
  
  test3()
  {
      settrap
      echo "test1"
      eval test2
  }
  </code></pre>
  
  <p>What will happen?</p>
  
  <ul>
  <li>test1 will exit but not call myexit</li>
  <li>test2 and test3 will call myexit.</li>
  </ul>
  
  <p><strong>Question</strong>: Why does test1 not cause myexit to be called when the call to test2 returns -1?</p>
  
  <hr>
  
  <p>Edit: The problem is not because functions have local traps. As explained <a href="https://docstore.mik.ua/orelly/unix3/korn/ch08_04.htm" rel="nofollow noreferrer">here</a>: Old-style POSIX functions (those created using the name() syntax) share traps with the parent script. </p>