I have the following function to colorize the stderr output of a command:
red=`tput setaf 1`
colorerr() {
(trap 'tput sgr0' EXIT; eval "$* 2> >(echo -n \"${red}\"; cat -;)")
}
e.g.:
colorerr "bash -c 'cd ${WEB_APP_DIR}; npm run-script build'"
The script uses set -e, trap ... ERR, and trap ... EXIT to report line numbers and stack traces on failures. Everything is working fine, except if the command string passed into colorerr fails, I wanted to catch the error and give better reporting (since telling me that a failure happend in the colorerr function isn't useful).
I tried the standard || ... construction, but the error from the subshell within colorerr was still triggering the ERR trap. As a test, I tried the following:
(trap 'tput sgr0' EXIT; eval "$* 2> >(echo -n \"${red}\"; cat -;)") ||
true
But the ERR trap is still triggered. Why isn't the || true setting the error code to '0' as it normally does?