相关疑难解决方法(0)

为什么我的变量在一个“while read”循环中是局部变量,而在另一个看似相似的循环中却不是?

为什么我$x从下面的片段中得到不同的值?

#!/bin/bash

x=1
echo fred > junk ; while read var ; do x=55 ; done < junk
echo x=$x 
#    x=55 .. I'd expect this result

x=1
cat junk | while read var ; do x=55 ; done
echo x=$x 
#    x=1 .. but why?

x=1
echo fred | while read var ; do x=55 ; done
echo x=$x 
#    x=1  .. but why?
Run Code Online (Sandbox Code Playgroud)

pipe shell-script variable-substitution variable

28
推荐指数
3
解决办法
3万
查看次数