为什么 bash eval return 0 on command exiting with status > 0?
Why does bash eval return 0 on command exiting with status > 0?
我无法理解这样一个事实,即在命令执行中使用 Bash eval
显然退出状态 > 0 仍然 returns 与 0。考虑这个示例:
> eval "$(exit 42)"
> echo $?
0
单独的命令 $(exit 42)
的退出代码 > 0,因此根据 man
页面,eval
应该 return 本身的退出状态为 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.
我误会了什么?
再次阅读问题和手册摘录,在调用 eval
之前扩展了 eval 的参数。
"$(exit 42)"
扩展为空字符串,命令变为 eval ''
,成功退出。
set -x
可用于追踪正在发生的事情
set -x
> eval "$(exit 42)"
++ exit 42
+ eval ''
然而
> x=$(exit 42)
++ exit 42
+ x=
> echo "$?"
+ echo 42
42
另请参阅单引号不同,因为扩展是由 eval eval '$(exit 42)'
returns 42
> eval '$(exit 42)'
+ eval '$(exit 42)'
+++ exit 42
我无法理解这样一个事实,即在命令执行中使用 Bash eval
显然退出状态 > 0 仍然 returns 与 0。考虑这个示例:
> eval "$(exit 42)"
> echo $?
0
单独的命令 $(exit 42)
的退出代码 > 0,因此根据 man
页面,eval
应该 return 本身的退出状态为 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.
我误会了什么?
再次阅读问题和手册摘录,在调用 eval
之前扩展了 eval 的参数。
"$(exit 42)"
扩展为空字符串,命令变为 eval ''
,成功退出。
set -x
可用于追踪正在发生的事情
set -x
> eval "$(exit 42)"
++ exit 42
+ eval ''
然而
> x=$(exit 42)
++ exit 42
+ x=
> echo "$?"
+ echo 42
42
另请参阅单引号不同,因为扩展是由 eval eval '$(exit 42)'
returns 42
> eval '$(exit 42)'
+ eval '$(exit 42)'
+++ exit 42