我发现 4.2 的行为是正确的,因为在评估作业之前应该首先进行分词:
IFS=, cat <<< "${foo[*]}"
我相信 IFS 的新值应该只会影响cat
并不是<<< "${foo[*]}"
.
正确的方法是
IFS=, eval 'cat <<< "${foo[*]}"'
对于保守的方法,我们可以使用一个函数:
function t { cat <<< "${foo[*]}"; }
IFS=, t
我也猜测与此有关:
m. Fixed a bug that caused here documents to not be displayed correctly
when attached to commands inside compound commands.
Update
另一个令人困惑的行为可以在 4.2 中找到,该行为已在 4.3 中修复。通过做IFS=, cat <<< "${foo[*]}"
, "${foo[*]}"
扩展得很好"111 222 333"
(不受 IFS 影响)如下所示cat
这是111 222 333
。然而当我们这样做时IFS=, read bar <<< "${foo[*]}"; echo "$bar"
,输出将是111,222,333
看起来好像"${foo[*]}"
扩展为"111,222,333"
。这种不一致的行为在 Bash 4.3 中不再发生,并且 Bash 4.3 的行为可能是一个修复。