On Tue, Aug 22, 2023, at 11:09 AM, crstml@libero.it wrote:
Let's assume the following script named test-subst-and-error.sh:
 1 func()
 2 {
 3     echo "entering func"
 4     : $-
 5     bash -c "echo XXXXXXXX; exit 8"
 6     echo "exiting func"
 7 }
 8
 9  OUTPUT=$(cat non-existing-file.txt)
10Â Â # OUTPUT=$(func)
11
12Â Â echo "OUTPUT: '${OUTPUT}'"
[...]
My only question is: why in the case 1 when external program
is called errexit is not not automatically disabled but it is
disabled when a function is called?
set -e is disabled within both command substitutions. The difference
is that the exit status of the assignment on line 9 is nonzero,
causing early exit, while that of the assignment on line 10 is zero
(unless the "echo" command on line 6 fails for some reason).