为什么读**而忽略标准输入[重复]

问题描述 投票:1回答:1

为什么使用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个回答
0
投票

他们俩都很好。问题在于您的第二个示例必须在单独的过程中调用read。在该单独的过程中,spanishWords包含正确的内容。但这对您没有帮助。

这将起作用:

printf "uno\tdos\n" |
   ( read -a spanishWords;
     echo "${spanishWords[0]}" )
© www.soinside.com 2019 - 2024. All rights reserved.