如果存在set -e指令,为什么((0))导致Bash脚本退出? [英] Why does ((0)) cause a Bash script to exit if `set -e` directive is present?

查看:58
本文介绍了如果存在set -e指令,为什么((0))导致Bash脚本退出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这会输出 before \ n :

 #!/usr/bin/env bash设置-e回声之前((0))回声之后 

删除 set -e 或将 [[(0))更改为((1))可使程序输出 before \nafter \ n 符合预期.

为什么((0))触发 set -e 退出条件?

解决方案

这将说明:

 ((0))回声$?1个((1))回声$?0 

所以这是由于((和))中算术表达式求值的非零返回状态所致,在使用 set -e 时您的脚本正在退出./p>

帮助集这样说:

-e如果命令以非零状态退出,则立即退出.

This outputs before\n:

#!/usr/bin/env bash
set -e

echo before
((0))
echo after

Removing set -e or changing ((0)) to ((1)) makes the program output before\nafter\n as expected.

Why does ((0)) trigger the set -e exit condition?

解决方案

This will explain:

((0))
echo $?
1

((1))
echo $?
0

So it is due to non-zero return status of arithmetic expression evaluation in (( and )) your script is exiting when set -e is being used.

As help set says this:

-e Exit immediately if a command exits with a non-zero status.

这篇关于如果存在set -e指令,为什么((0))导致Bash脚本退出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆