This post basically asks the same question but I want an additional requirement, which is: the exit code should be unchanged.
The accepted answer uses pipe, so the exit code is no longer the exit code of command1.
Command set pipefail doesn't fit my need as I don't want to affect the behavior when executing command1, which might be a compound Bash command itself.
I tried the following approach, but the output is not as expected:
[hidden]$ (echo haha; echo hehe 1>&2) > >(while read -r x; do echo "xx $x"; done)
2> >(while read -r y; do echo "yy $y"; done)
xx haha
xx yy hehe
Can anyone tell why?
 
     
     
    