3

Am experimenting a bit with trap on invalid command return code using a sample code

#!/bin/bash

# Exit on error
trap 'echo 'exiting..';exit' ERR
set -e

h=1
b=$((h+)) # <----- command causing the error
echo $?
echo $b
echo "end"

Am not able to cause the trap to occur even though there is an offending instruction. But the same if I run in a sub-shell as

function junk() {
h=1
b=$((h+))
echo "Exit code:$?"
echo $b
echo "end"
echo "Hello"
}

junk

(or) running the entire instructions in a sub-shell as

(h=1
b=$((h+))
echo "Exit code:$?"
echo $b
echo "end"
echo "Hello"
)

am able to catch the command failure and the EXIT catches the trap and prints the message accordingly.

Am aware of the usage of set -e

-e  errexit When set, the shell exits when a simple command in a command list exits
    non-zero (FALSE). This is not done in situations, where the exit code is already checked 
    (if, while, until, ||, &&)

but I can't find a proper reference where it says happens only on a sub-shell or a function or something similar.

Let me know if I am missing something basic here.

1
  • Your trap 'echo 'exiting..';exit' ERR doesn't nest quotes, so you're popping out of quotes for the word exiting... and then back in again. I suspect that although this works it's not actually what you intend. I would suggest that trap 'echo "exiting..";exit' ERR might be better. Commented Dec 29, 2016 at 13:56

1 Answer 1

6

What you are missing is that set -e and sigspec ERR both apply to command execution (commands that exit with a non-zero value). What you have here with b=$((h+)) is a parsing error. The command is not executed because it is not understood.

Why does it work in a function or sub-shell? Because this parsing error makes the containing script (be it function or a sub-shell) fail as a whole. In other words, this is not b=$((h+)) that triggers your trap (there is no trap set in your sub-shells), this is the failure of the call to junk or to your subshell.

7
  • That's great! Can you provide some reference from any of the man pages about this behavior, I'd be happy to accept it!
    – Inian
    Commented Dec 29, 2016 at 13:55
  • @Inian since it's not valid syntax perhaps you could explain what you would have expected b=$((h+)) to do. Commented Dec 29, 2016 at 13:57
  • @roaima : Note sure what you meant, that line was the intended offending instruction which I enforced manually.
    – Inian
    Commented Dec 29, 2016 at 14:00
  • @roaima : Wanted a proper reference as per xhienne's answer, if the containing script either a function or a subshell could cause the failure as a whole rather than a single offending instruction
    – Inian
    Commented Dec 29, 2016 at 14:13
  • 2
    @Inian oh I see. I thought you wanted a reference to the failing command. Anyway, here is the part of the bash manual that describes this behavour: « If a sigspec is ERR, the command arg is executed whenever a pipeline (which may consist of a single simple command), a list, or a compound command returns a non-zero exit status [...] » Commented Dec 29, 2016 at 14:24

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .