Reputation: 311
How to preserve the change in a variable that made in the child process for the parent process in a bash cell?
I define a variable as follows:
var=10
I export it to access it in the child process.
export var
Now I create a child process and change $var in the child process.
bash
var=20
The change in $var preserves until the child process exit. when I exit the child process change is also overwritten. I want to preserve that change for the parent process. Tell me how to do this?
Upvotes: 2
Views: 1441
Reputation: 1322
The child gets its own copy of variables, so it can't change them for other processes - not even the parent. The most straightforward way (probably) of passing something to the parent would be using a temporary file.
Upvotes: 2
Reputation: 362037
Child processes cannot change environment variables in their parents. Children get copies of their parents' environments and any changes are only to the children's copies.
If you want to affect the parent you'll have to communicate to it in some way. That could be via a pipe or a UNIX socket or shared memory or some other form of interprocess communication. The easiest method may be to write the new value to stdout and have the parent read that and change the variable itself.
Upvotes: 5