Question: Im trying to read an exported variable in c but i cant, there are two scenarios.

**A)**

1) i have a sh file like:

```
#!/bin/bash
timeout 3s ./program
export OUT=$?

```

2) then i run: `. file.sh`

3) and when i read the var in c with:

`getenv("OUT")` yes, i read it.

**B)**

1. in c: `system("/bin/bash -c '. PathOfFile/file.sh'");`
2. `getenv("OUT")` . i cant access to OUT

i need run the sh file in c and read the var also in c

thanks

Answer: 
In case 'B' sh file will run in new process.

To make it more clear what you can do is:

1. Open two terminal windows.
2. Export your variable in one window.
3. Now run your c program in another window.

You'll find that you can not access that variable in second terminal window.In case 'A' you were being able to access the variable because you were running your c program from same terminal window(which make your c program a child process of terminal session).All child process inherit environment variables from parent.As others mentioned that you can see complete process tree using **pstree** command and you will find **init** as root of the tree.

What you can do is make another c file which contains:

system("/bin/bash -c '. PathOfFile/file.sh'");

and in file.sh first export your variable and then run actual c file.

Another way is to use IPC.
You must understand the concept of process. `system("/bin/bash -c '. PathOfFile/file.sh'");` will create a new process for you to run your file.sh, and the `OUT` environment variable is declared in your file.sh, in other word, it exists in the context of the new process. However, your just can't get the environment variable in your C code (which call `system` function), because it's totally different process context. Of course you can still use IPC methods to get this environment variable if you want.