In arrays, bash considers expressions between []
as arithmetic. Thus
i=2 ; f[i++]=10
is perfect. Writing f[((i++))]
is also correct but in this case, (())
is not seen as the arithmetic expansion operator, but as nested parentheses.
Note that ((expr))
evaluates expr
, then succeeds if it is true, while$((expr))
is expanded as its value. So f[$((i++))]
is also correct.
Finally, f[$i++]
is not what you want since $i
is expanded first. For instance, i=j ; f[$i++]
will be expanded as f[j++]
.
Remark: a strange feature is that bash expands all it can in arithmetic mode without the $
sign:
$ unset i j k f
$ i=j ; j=k ; k=5 ; f[i++]=10
$ declare -p i j k f
declare -- i="6"
declare -- j="k"
declare -- k="5"
declare -a f='([5]="10")'
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…