Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

change environment variable in child process - bash

Hi I have following example. a.sh script sets environment variable which I can see in b.sh (child) script, but if I change it I still have the old value in a.sh

a.sh

#!/bin/bash

export A=1
./b.sh
echo parent $A

b.sh

#!/bin/bash

echo child $A
A=2
export A
echo child $A

test:

bash-3.00$ ./a.sh
child 1

child 2

parent 1

child 1

child 2
like image 897
Gayane Avatar asked Sep 14 '25 21:09

Gayane


1 Answers

In a.sh do source b.sh instead of ./b.sh

a.sh should look like this :

#!/bin/bash
export A=1
source b.sh
echo parent "$A"
like image 200
iamauser Avatar answered Sep 16 '25 12:09

iamauser