Linux - bash while subshell中变量操作的问题

Linux - bash while subshell中变量操作的问题

需求

演示一个简单的需求:使用bash语言,对1到5进行累加,并输出结果。

代码

$ cat while_loop_operate_global_variable.sh
#!/bin/bash
# Author: thesre
# Date: 

total=0
echo "Let's check the initial value of total:"
echo "$total"
echo ""

num_list=`seq 1 5`

echo "$num_list"| while read i
do
    total=`echo $total + $i| bc`
    echo $total
done
echo ""
echo "OK, let's check the value of total out of the while loop."
echo $total

Run结果

[thesre@centos8 ~]$ ./while_loop_operate_global_variable.sh
Let's check the initial value of total:
0

1
3
6
10
15

OK, let's check the value of total out of the while loop.
0

可以看到,在while loop中有对变量进行累加。但,最终结果,却是初始值0!

这是为何?

分析

这是因为while运行在subshell里,当while运行完后,其里面的变量被废弃。因此在最后打印的还是初始值。

解决

要解决该问题,需要避免在subshell中操作变量。

方法一:我们可以使用here string的方式来让操作变量的代码放在脚本main shell中。

[thesre@centos8 ~]$ cat while_loop_operate_global_variable.sh
#!/bin/bash
# Author: thesre
# Date: 

total=0
echo "Let's check the initial value of total:"
echo "$total"
echo ""

num_list=`seq 1 5`

while read i
do
    total=`echo $total + $i| bc`
    echo $total
done <<< "$num_list"

echo ""
echo "OK, let's check the value of total out of the while loop."
echo $total

方法二:我们可以使用process substitution的方式来让操作变量的代码放在脚本main shell中。

#!/bin/bash
# Author: thesre
# Date: 

total=0
echo "Let's check the initial value of total:"
echo "$total"
echo ""

while read i
do
    total=`echo $total + $i| bc`
    echo $total
done < <(seq 1 5)

echo ""
echo "OK, let's check the value of total out of the while loop."
echo $total

Run效果

[thesre@centos8 ~]$ ./while_loop_operate_global_variable.sh
Let's check the initial value of total:
0

1
3
6
10
15

OK, let's check the value of total out of the while loop.
15

总结

在脚本程序出现不符合预期的情况时,需分析进程间的关系。如本案,操作变量的代码在subshell中,无论如何操作都不会影响在main shell中的变量。

参考资料

https://tldp.org/LDP/abs/html/gotchas.html#BADREAD0 #管道输出到任一命令均会有该问题
https://tldp.org/LDP/abs/html/process-sub.html #process substitution

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

王万林 Ben

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值