此问题已经在这里有了答案:
为什么使用read命令手动将值输入为数组:
read -a words
## type values here and then enter
但这不是:
printf "uno\tdos\n" | read -a spanishWords
echo "${spanishWords[0]}" ## This is empty
他们俩都很好。问题在于您的第二个示例必须在单独的过程中调用read
。在该单独的过程中,spanishWords
包含正确的内容。但这对您没有帮助。
这将起作用:
printf "uno\tdos\n" |
( read -a spanishWords;
echo "${spanishWords[0]}" )