Failed $(<file) exits under 'set -e' even when followed by ||
From:
Zev Weiss
Subject:
Failed $(<file) exits under 'set -e' even when followed by ||
Date:
Fri, 31 Mar 2023 19:20:35 -0700
Configuration Information [Automatically generated, do not change]:
Machine: x86_64
OS: linux-gnu
Compiler: gcc
Compilation CFLAGS: -g -O2
uname output: Linux hatter 5.18.19_1 #1 SMP PREEMPT_DYNAMIC Thu Aug 25 14:36:55
UTC 2022 x86_64 GNU/Linux
Machine Type: x86_64-pc-linux-gnu
Bash Version: 5.2
Patch Level: !PATCHLEVEL!
Release Status: release
Description:
When 'set -e' is enabled, file-content substitutions of the form $(<...)
cause an immediate exit even when the subsitution is in a conditional
context such as the condition of an 'if' statement or the left operand
of the '||' operator.
I'm guessing this probably has something to do with the fork-suppression
mentioned in the release notes for bash 5.2, since the problem seems to
have appeared with that release (5.1.16 does not exhibit the bug).
In prior releases this produced an error message for the nonexistent
file but then continued to print 'hello'; in bash 5.2 and later it exits
with a non-zero exit status immediately after issuing the error message
without proceeding to the 'echo' command.
The same behavior can be seen with an 'if' statement instead:
$ bash -e -c 'if ! x=$(</nosuchfile); then echo hello; fi'
Thanks,
Zev Weiss