Skip to content
Advertisement

Setup or env variable lost after running bash files

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
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement