【问题标题】:How does `read` saves the output in the `$REPLY` variable [duplicate]`read`如何将输出保存在`$REPLY`变量中[重复]
【发布时间】:2022-01-26 04:25:19
【问题描述】:

我想知道为什么我无法从管道的输出中读取该行。以下是我的命令:

find / -name sysinit.target 2> /dev/null | head -n1 | read

我希望找到从head 的输出中读取的行记录在系统变量$REPLY 中,但该变量中没有任何内容。为什么head输出的行没有被read记录在变量$REPLY中?

【问题讨论】:

  • 这也是BashFAQ #24的主题。
  • head -n1 也完全不需要。

标签: bash


【解决方案1】:

它不起作用,因为管道是在子 shell 中运行的。查看lastpipe shell 选项以解决它或仅使用进程替换。

read < <(...)

也可以作为建议使用IFS=-r

【讨论】:

    猜你喜欢
    • 2013-09-09
    • 2021-10-10
    • 1970-01-01
    • 2019-11-05
    • 1970-01-01
    • 2011-11-10
    • 1970-01-01
    • 2020-03-29
    • 1970-01-01
    相关资源
    最近更新 更多