当'let'表达式求值为0时,为什么Bash with -e选项会退出?

PBr*_*ann 2 bash arithmetic-expressions

我很难理解为什么bash -e选项退出此脚本.只有在计算的表达式给出时才会发生0:

#!/bin/bash
set -ex
table_year=( 1979 1982 1980 1993 1995 )
year=$1 
let indice=year-1
real_year=${table_year[$indice]}
echo OK $real_year
Run Code Online (Sandbox Code Playgroud)

是的还可以:

./bash_test_array 2
Run Code Online (Sandbox Code Playgroud)

但不是在:

./bash_test_array 1 
Run Code Online (Sandbox Code Playgroud)

indice这种情况等于0.为什么-e选项导致退出?

Mic*_*ros 5

help let:

退出状态:如果最后一个ARG评估为0,则返回1; 让否则返回0 ..

let内置的行为与常用expr命令的行为相同:

如果EXPRESSION为空或0 [...],退出状态为[...] 1

您可以使用算术扩展:

indice=$(( year - 1 ))
Run Code Online (Sandbox Code Playgroud)

即使指定的表达式求值为0,此语句也将返回0.