我最近发现的一件奇怪的事:至少在我的 bash (4.2) 版本中,条件运算符似乎不会正常评估未采用的路径,但它对数组索引表达式会这样做。例子:
$ echo $i # i initially unset
$ ((0?++i:0)); echo $i # As expected, i is not touched when false branch taken, like any sane language
$ ((0?a[++i]:0)); echo $i # But when untaken branch indexes, and indexing expression has side-effect... WTF?!?
1
$ ((0?a[++i]:0)); echo $i # Continues building on each call...
2
至少可以说,这是相当令人惊讶的。它的不一致感觉
csh
-like。这是我特定版本的 bash 中的错误,还是由于算术评估和/或数组索引规则的交互(其中数组索引本身已经强加了算术评估规则)以某种方式需要它在某些比 更详细的规范中man bash
? (或者我是否错过了 Bash 手册页中要求这样做的地方?)