Set a parent shell's variable from a subshell

后端 未结 7 1460
野性不改
野性不改 2020-11-27 03:58

How do I set a variable in the parent shell, from a subshell?

a=3
(a=4)
echo $a
7条回答
  •  無奈伤痛
    2020-11-27 04:16

    If the problem is related to a while loop, one way to fix this is by using Process Substitution:

        var=0
        while read i;
        do
          # perform computations on $i
          ((var++))
        done < <(find . -type f -name "*.bin" -maxdepth 1)
    

    as shown here: https://stackoverflow.com/a/13727116/2547445

提交回复
热议问题