本文介绍了IFS变化与巴什4.2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

运行这些命令给了预期的效果。

Running these commands gives expected results

$ bash --version
GNU bash, version 4.1.11(2)-release

$ foo=(111 222 333)

$ IFS=, cat <<< "${foo[*]}"
111,222,333

但它似乎与猛砸4.2 IFS值被忽略

However it appears with Bash 4.2 the IFS value is being ignored

$ bash --version
GNU bash, version 4.2.0(1)-release

$ foo=(111 222 333)

$ IFS=, cat <<< "${foo[*]}"
111 222 333

是什么导致了这种差异?

What is causing this difference?

我找到了答案在这里

看起来好像这一直都是一个错误。据切特
重定向不应该接触到TEMP环境
IFS 在这种情况下)

It looks as though this has been an error all along. According to Chet,redirections should never have had access to the temp environment(IFS in this case)

推荐答案

我觉得4.2正确的,因为分词的行为应该分配的评估之前,首先发生了:

I find the behaviour of 4.2 correct as word splitting should happen first before evaluation of assignments:

IFS=, cat <<< "${foo[*]}"

我相信IFS的新值只会影响,而不是&LT;&LT;&LT; $ {foo的[*]}

要做到这一点,正确的方法是

The proper way to do that is

IFS=, eval 'cat <<< "${foo[*]}"'

而对于保守的方法,我们可以使用一个函数:

And for a conservative approach we can use a function:

function t { cat <<< "${foo[*]}"; }
IFS=, t

我也有猜测,它是与此相关的:

I also have a guess that it's related to this:

m.  Fixed a bug that caused here documents to not be displayed correctly
    when attached to commands inside compound commands.

更新

另一个令人困惑的行为可在4.2这已经是固定在4.3中找到。通过这样做 IFS =,猫&LT;&LT;&LT; $ {foo的[*]}$ {foo的[*]}扩大以及111 222 333(不受IFS),通过 111 222 333 。然而,当我们做 IFS =读巴≤;&LT;&LT; $ {foo的[*]};回声$栏,输出会 111222333 键,会出现,如果$ {foo的[*]} 扩展为111222333。这种不一致的行为已不再猛砸4.3发生和Bash 4.3的行为可能是一个修补程序。

Update

Another confusing behavior can be found in 4.2 which is already fixed in 4.3. By doing IFS=, cat <<< "${foo[*]}", "${foo[*]}" expands well as "111 222 333" (not affected by IFS) as shown by cat which is 111 222 333. However when we do IFS=, read bar <<< "${foo[*]}"; echo "$bar", the output would be 111,222,333 and would appear as if "${foo[*]}" expanded as "111,222,333". This inconsistent behavior is no longer happening in Bash 4.3 and Bash 4.3's behavior is probably a fix.

这篇关于IFS变化与巴什4.2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 16:31