Why does ((0)) cause a Bash script to exit if `set -e` directive is present?

混江龙づ霸主 提交于 2021-02-08 16:59:10

问题


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?


回答1:


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.




回答2:


Line

 set -e

means: -e Exit immediately if a command exits with a non-zero status. (see: https://www.gnu.org/software/bash/manual/html_node/The-Set-Builtin.html)

((0)) is an expression that evaluates to 1. That's why the script exits.



来源:https://stackoverflow.com/questions/24148146/why-does-0-cause-a-bash-script-to-exit-if-set-e-directive-is-present

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!