【问题标题】:How to preserve newlines with cat command如何使用 cat 命令保留换行符
【发布时间】:2019-10-01 19:22:53
【问题描述】:

我有这个:

echo `cat << 'EOF'
    select c1, c2 from foo
    where c1='something'
EOF`

它将这个记录到标准输出:

select c1, c2 from foo where c1='something'

但我希望以某种方式保留换行符,因此它会输出:

select c1, c2 from foo  
where c1='something'

我该怎么做?

【问题讨论】:

  • 你为什么使用echo `...` 而不是仅仅运行cat

标签: bash shell cat heredoc


【解决方案1】:

echo 中使用双引号来保留字符串的原始格式。

echo "`cat << 'EOF'
    select c1, c2 from foo
    where c1='something'
EOF`"

【讨论】:

    【解决方案2】:

    你根本不需要echo

    cat <<'EOF'
    select c1, c2 from foo
    where c1='something'
    EOF
    

    【讨论】:

    • 这不是问题的重点,我认为@PS。有正确的想法
    • “我怎样才能[保留换行符]?”这是这样做的。你对无关的echo有充分的理由吗?
    • 你甚至不需要cat;只需使用带有嵌入换行符的常规字符串。
    • 好的,那听起来不错,那么您可以添加另一个问题的答案吗?
    猜你喜欢
    • 2018-11-22
    • 2014-09-26
    • 2018-04-02
    • 1970-01-01
    • 2014-04-01
    • 2013-05-11
    • 1970-01-01
    • 2017-04-09
    • 1970-01-01
    相关资源
    最近更新 更多