为什么" echo foo |读一个;回声$ a"不能按预期工作? [英] Why is "echo foo | read a ; echo $a" not working as expected?

查看:60
本文介绍了为什么" echo foo |读一个;回声$ a"不能按预期工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以在FreeBSD,GNU/Linux和Solaris下使用各种shell复制该问题.它让我头疼了一个多小时,所以我决定在这里发问.

I could replicate the problem with various shells under FreeBSD, GNU/Linux, and Solaris. It had me head-scratching for more than an hour, so I decided to post the question here.

推荐答案

由于使用了管道,因此read是在其自己的子shell中执行的.

Due to the piping the read is executed in its own subshell.

echo foo | while read a; do echo $a; done

将按照您的期望去做.

这篇关于为什么" echo foo |读一个;回声$ a"不能按预期工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆