且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在 Bash 中减去两个变量

更新时间:2023-11-29 19:07:28

你只需要在减号和反引号周围多加一点空格:

You just need a little extra whitespace around the minus sign, and backticks:

COUNT=`expr $FIRSTV - $SECONDV`

注意退出状态:

如果 EXPRESSION 既不为空也不为 0,则退出状态为 0,如果 EXPRESSION 为空或 0,则退出状态为 1.

The exit status is 0 if EXPRESSION is neither null nor 0, 1 if EXPRESSION is null or 0.

在将 bash 脚本中的表达式与 set -e 结合使用时请记住这一点,如果命令以非零状态退出,它将立即退出.

Keep this in mind when using the expression in a bash script in combination with set -e which will exit immediately if a command exits with a non-zero status.