2

我在最后一个命令行中发现以下 bash 行为很奇怪。对我来说,这完全出乎意料。

$ set | grep ^BASH_VERSINFO             # Show the array
BASH_VERSINFO=([0]="3" [1]="2" [2]="25" [3]="1" [4]="release" [5]="x86_64-redhat-linux-gnu")

$ echo "${BASH_VERSINFO[@]}"             # Print all array
3 2 25 1 release x86_64-redhat-linux-gnu

$ echo "${BASH_VERSINFO[@]%e}"           # Print all array removing final 'e' of every member
3 2 25 1 releas x86_64-redhat-linux-gnu

$ i=4; echo "${BASH_VERSINFO[i]}"        # Print an array member
release
$ i=4; echo "${BASH_VERSINFO[i++]}"      # Print an array member and increase the 'i' variable
release
$ i=4; echo "${BASH_VERSINFO[i]%e}"      # Print an array member removing the final 'e'
releas
$ i=4; echo "${BASH_VERSINFO[i++]%e}"    # Why does bash use the next member?
x86_64-redhat-linux-gnu

似乎 bash 预先增加了 'i' 变量。

类似的奇怪行为:

$ i=5; echo "${BASH_VERSINFO[--i]}"    # Print array member ${BASH_VERSINFO[4]}. 'i' is descreased one time.
release
$ i=5; echo "${BASH_VERSINFO[--i]%e}"  # Print array member ${BASH_VERSINFO[3]%e}. 'i' is descreased two times.
1

有谁可以告诉我这种行为?

提前感谢

4

1 回答 1

5

这似乎是一个已修复的错误,可能是这个:

(来自bashChangeLog ——bash-4.2-alpha 和 bash-4.1-release 之间的变化)

修复了算术扩展中导致数组扩展中的索引在某些情况下被计算两次的错误。

工作表:

$ echo "${BASH_VERSINFO[*]}"
4 2 42 1 release i586-pc-linux-gnu
$ i=4; echo "${BASH_VERSINFO[i++]}"; echo "$i"
release
5
$ i=4; echo "${BASH_VERSINFO[i++]%e}"; echo "$i"
releas
5
于 2013-08-13T06:44:33.967 回答