Set a parent shell's variable from a subshell

后端 未结 7 1474
野性不改
野性不改 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:12

    To change variables in a script called from a parent script, you can call the script preceded with a "."

    a=3
    echo $a    
    . ./calledScript.sh
    echo $a
    

    in calledScript.sh

    a=4
    

    Expected output

    3
    4
    

提交回复
热议问题