我的问题是自我描述:
script1.sh包含:
#!/bin/sh
export SOMEVAR=0
echo "1, SOMEVAR=$SOMEVAR"
./script2.sh
echo "2, SOMEVAR=$SOMEVAR"
script2.sh包含:
#!/bin/sh
SOMEVAR=1
./ script1.sh给出:
1, SOMEVAR=0
2, SOMEVAR=0
虽然我想:
1, SOMEVAR=0
2, SOMEVAR=1
我如何获得想要的结果?
答案 0 :(得分:4)
使用source
(又名。.
)而不是调用脚本,子进程无法更改父项环境:
#!/bin/sh
export SOMEVAR=0
echo "1, SOMEVAR=$SOMEVAR"
. ./script2.sh
echo "2, SOMEVAR=$SOMEVAR"