【问题标题】:Why is **read** ignoring stdin [duplicate]为什么**读取**忽略标准输入[重复]
【发布时间】:2020-03-14 06:20:42
【问题描述】:

为什么使用 read 命令手动将值作为数组输入:

read -a words
## type values here and then enter

但这不是:

printf "uno\tdos\n" | read -a spanishWords
echo "${spanishWords[0]}" ## This is empty

【问题讨论】:

标签: bash stdin


【解决方案1】:

它们都工作得很好。问题是您的第二个示例在单独的进程中调用read。在那个单独的过程中,spanishWords 包含正确的内容。但这对你没有帮助。

这可行:

printf "uno\tdos\n" |
   ( read -a spanishWords;
     echo "${spanishWords[0]}" )

【讨论】:

  • 我不会说“必要”; ksh 没有,bash 也没有,如果您使用的是 lastpipe 选项。
  • @chepner 谢谢。我把那个词去掉了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-11-23
  • 1970-01-01
  • 2021-01-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多