title: Trap syntax issue in bash
body: <p>I intend to use trap to execute some clean up code in case of a failure. I have the following code, but it seems to be have some syntactical issues.</p>
  
  <pre><code>#!/bin/bash
  
  set -e
  
  function handle_error {
      umount /mnt/chroot
      losetup -d $LOOP_DEV1 $LOOP_DEV2
  }
  
  trap "{ echo \"$BASH_COMMAND failed with status code $?\"; handle_error; }" ERR
  </code></pre>
  
  <p>Does any one see an issue with the way the trap has been written. In case of an error the trap does get executed fine but it also throws another unwanted error message below.</p>
  
  <pre><code>/root/myscript.sh: line 60: } ERR with status code 0: command not found
  ##line 60 is that line of code that exited with a non zero status
  </code></pre>
  
  <p>How do I write it correctly to avoid the error message? Also what if I had to send arguments $LOOP_DEV1 and $LOOP_DEV2 from the main script to the trap and then to the handle_error function? Right now they are exported as environment variables in the main script. I did some search for trap examples but I couldn't get something similar.</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>I changed the shebang from /bin/sh to /bin/bash. As /bin/sh was already symlinked to bash I did not expect unicorns nor did I see any.</p>