从管道中将值读入shell变量

从管道中将值读入shell变量

我试图让bash处理来自stdin的数据,但是没有运气。我的意思是以下工作:


echo "hello world" | test=($(< /dev/stdin)); echo test=$test

test=


echo "hello world" | read test; echo test=$test

test=


echo "hello world" | test=`cat`; echo test=$test

test=

我想要输出的地方test=hello world。我试过把“”引号括起来"$test"也不起作用。


沧海一幻觉
浏览 1300回答 3
3回答

慕妹3242003

read不会从管道读取(或者由于管道创建子壳,结果可能会丢失)。但是,您可以在Bash中使用here字符串:$ read a b c <<< $(echo 1 2 3)$ echo $a $b $c1 2 3但请参阅@ chepner的答案以获取相关信息lastpipe。
打开App,查看更多内容
随时随地看视频慕课网APP