我想读取命令输出的第一行。但我收到一条空消息。
我用过
ls | read line
echo $line #nothing displayed
为什么 line 变量为空,如何解决?
以下代码有效。但我只想读第一行
ls | while read line; do
echo $line
done
如果无法使用 read,是否可以使用 grep、awk、sed 等其他函数来完成?
最佳答案
read
确实读取了第一行,但它是在子 shell 中执行的。但你可以这样做:
ls | { read line;
echo $line;
# other commands using $line;
}
# After the braces, $line is whatever it was before the braces.
关于linux - 为什么 "read"不读取命令输出的第一行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17925892/