Please excuse me if there is an error in the sample code.
There are two files testA.sh and testB.sh.
testA.sh
#!/bin/bash export a="hello" source testb.sh
testB.sh
#!/bin/bash echo "test b" echo $a
deepak[18:33] $ ./testA.sh
test b
hello
deepak[18:33] $ echo $a
deepak[18:33] $
If we run any setup in the testB.sh then how can we use that setup or env variable in the main console (for example echo $a)
Advertisement
Answer
testA.sh runs in a subshell, and it’s environment evaporates with it when it ends.
if you source testA.sh
it will keep the value.
$: ./testA.sh test b hello $: echo $a $: . ./testA.sh test b hello $: echo $a hello