linux 管道产生子shell

linux中使用管道,下一个命令会放在子shell中执行,子shell中是不能访问主shell的变量

hadoop@hadoop:~$ cat file.txt
hanxin

hadoop@hadoop:~$ me=weihongrao
hadoop@hadoop:~$ cat file.txt | while read line;do me=$line;echo "inner me is:${me}";done;echo "outer me is :${me}";
inner me is:hanxin
outer me is :weihongrao
hadoop@hadoop:~$

以上在外部定义了一个变量me值为:weihongrao, 文件file.txt中有另一个名字hanxin,想要用file.txt中的名字替换主shell中的me变量,如果用管道的办法如上是行不通的因为管道产上子shell后while在子shell中执行,子shell 不能访问主shell中的me变量,所以结果如上达不到目的。


方案1

hadoop@hadoop:~$ while read line;do me=$line;echo "inner me is :${line}";done<file.txt;echo "outer me is :${me}";
inner me is :hanxin
outer me is :hanxin
hadoop@hadoop:~$

方案2;

exec 3<&0;。。。。。。。略

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值