【问题标题】:Strangeness in bash: different behaviour inside heredocbash 中的奇怪之处:heredoc 内部的不同行为
【发布时间】:2014-05-12 17:45:25
【问题描述】:

我在使用 bash 脚本时遇到了一个奇怪的问题。重现它的最小代码在这里:

#!/bin/bash

function f() {
    IFS=. read a b <<<"$1"
    echo "a=$a b=$b"
}

f a.b
echo "inside echo: `f a.b`"
cat <<EOT
Inside heredoc: `f a.b`
EOT

预期的输出是:

a=a b=b
inside echo: a=a b=b
Inside heredoc: a=a b=b

但我明白了:

a=a b=b
inside echo: a=a b=b
Inside heredoc: a=a.b b=

我在这里错过了什么?

更新: 我的 Mac (3.2.48) 上的 bash 按预期工作。 我的 debian stable(4.2.37(1)-release)上的 bash 给出了所描述的奇怪。

【问题讨论】:

    标签: bash function heredoc ifs


    【解决方案1】:

    这是本地环境更改如何与此处的字符串交互的错误,已在即将发布的 4.3 版中修复。

    # bash 3.2
    % bash tmp.bash
    a=a b=b
    inside echo: a=a b=b
    Inside heredoc: a=a b=b
    
    # bash 4.2
    % /usr/local/bin/bash tmp.bash
    a=a b=b
    inside echo: a=a b=b
    Inside heredoc: a=a.b b=
    
    # bash 4.3, release candidate
    % bash/bash tmp.bash
    a=a b=b
    inside echo: a=a b=b
    Inside heredoc: a=a b=b
    

    【讨论】:

    • 感谢您指出这是 bash 中的错误,而不是我理智中的错误。
    猜你喜欢
    • 1970-01-01
    • 2019-02-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-01
    • 1970-01-01
    • 2020-04-21
    • 2017-08-06
    相关资源
    最近更新 更多