Why is set -e also ignored for functions called via command substitution
0
votes
0
answers
26
views
I've seen questions about this topic, but they talk about constructs like
if
or ||
.
I don't understand why the same behavior seem to happen for substitution ($()
)?
$ my_function() { echo "the following command could fail:"; false; echo "this is after the command that fails"; }
$ (set -ex; var=$(my_function); echo "$var" )
++ my_function
++ echo 'the following command could fail:'
++ false
++ echo 'this is after the command that fails'
+ var='the following command could fail:
this is after the command that fails'
+ echo 'the following command could fail:
this is after the command that fails'
the following command could fail:
this is after the command that fails
vs
$ my_function() { set -e; echo "the following command could fail:"; false; echo "this is after the command that fails"; }
$ (set -ex; var=$(my_function); echo "$var" )
++ my_function
++ set -e
++ echo 'the following command could fail:'
++ false
+ var='the following command could fail:'
errexit
seems to behave as expected for simple commands:
$ (set -ex; var=$(false); echo "$var" )
++ false
+ var=
If I use read
instead I get the expected behaviour:
$ my_function() { echo "the following command could fail:"; false; echo "this is after the command that fails"; }
$ (set -exo pipefail; my_function | read var; echo "$var" )
+ my_function
+ echo 'the following command could fail:'
+ read var
+ false
Asked by Jakub Bochenski
(325 rep)
Apr 29, 2025, 03:30 PM
Last activity: Apr 29, 2025, 03:36 PM
Last activity: Apr 29, 2025, 03:36 PM