Reputation: 8624
I can't get my mind around the fact that using Bash eval
on a command execution that clearly exits with status > 0 still returns with 0. Consider this example:
> eval "$(exit 42)"
> echo $?
0
The command $(exit 42)
alone has exit code > 0, so according to the man
page, eval
should return itself an exit status of 42
...
eval [arg ...]
The args are read and concatenated together into a single command. This command is
then read and executed by the shell, and its exit status is returned as the value
of eval. If there are no args, or only null arguments, eval returns 0.
What do I misunderstand?
Upvotes: 1
Views: 3587
Reputation: 19315
Reading again the question and the extract from manual, the argument of eval is expanded before eval
being called.
"$(exit 42)"
expands to empty string and command becomes eval ''
which exits with success.
set -x
can be used to trace what's happening
set -x
> eval "$(exit 42)"
++ exit 42
+ eval ''
however
> x=$(exit 42)
++ exit 42
+ x=
> echo "$?"
+ echo 42
42
See also that single quotes are different because expansion is processed by eval eval '$(exit 42)'
returns 42
> eval '$(exit 42)'
+ eval '$(exit 42)'
+++ exit 42
Upvotes: 4