[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
errortracing inside || and eval
From: |
Tycho Kirchner |
Subject: |
errortracing inside || and eval |
Date: |
Wed, 30 Nov 2022 14:56:51 +0100 |
Hi!
We would like to run an error trap inside a function, even if it is called within
|| -> func || true.
While bash's manpage documents in the trap-section that no error trap runs
"if the failed command ... is part of a command executed in ||", we found the following
workaround, using "eval":
test.sh
_________________________________-
func(){
# works if inside 'eval', otherwise trap '..' ERR has no effect.
eval "trap 'echo local error trap >&2;return 42' ERR; false"
echo func
return 3
}
set -o errtrace
trap 'echo global error trap >&2; exit 1' ERR
func || echo bye $?
_________________________________
$ ./test.sh
local error trap
bye 42
Is this a bug or expected behavior, we can rely on?
Thanks in advance
Tycho and Konstantin
- errortracing inside || and eval,
Tycho Kirchner <=