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: 
Many would refuse to admit it, but most of *us* have the exact same question just after discovering the environment variables. Today, it's seems "so evident" but at that time it wasn't easy. So I will try to explain things the most clearly and simply as I can.

First, as someone else said before, you must know the concept of *process*. I won't go into the details, but each time you launch an executable program, you create a new process. In Unix/Linux world, process are organized in strict *tree hierarchy*. That is all process having one unique ultimate ancestor called `init`. `init`has launched some processes on startup. In their turn those processes have launched others, and so on, down until that *shell* where you launch yourself some other processes. So:

* When you launch your compiled program, you create a new process
(having your current shell as father).
* When you call `system` to
execute a script you create a new process `sh` (having the host
program as father)
* When your shell script call an external
program/command it creates a new process (having the script's
execution shell as parent)

As you understand, the hierarchy might go deep fast. If you have the command `pstree` on your system, you might call it from a shell to display the current process hierarchy. This might help you to get grasp on it.

One key feature is that any process has an *environment* -- constituted by the set of its environment variables and their corresponding values.

And here is the big secret: when a process launch an other process, **the new process inherit a *copy* of the environment of its father**. Since it is a copy, **the *child process* can do what it want with its own environment, this won't alter it's father environments**. Nor its grand-father, nor its grand-grand-father, nor...)

---

So the moral of the story is, **a process cannot use environment variables to communicate with any of its ancestors**.

As this is your need, you would have to rely to an other strategy:

* You might want to communicate through [standard streams](http://en.wikipedia.org/wiki/Standard_streams) (`stdout`) as a process can *read* its child's *output*. Or (this is in fact a variation) use a [*named pipe*](http://en.wikipedia.org/wiki/Named_pipe).
* If you want something a little bit more elaborated, modern systems have a bunch of [IPC](http://en.wikipedia.org/wiki/Inter-process_communication), like [shared memory](http://en.wikipedia.org/wiki/Shared_memory). But those are not really suitable to communicate with a *shell script*.
* Finally, you might rely on (more or less) dedicated systems: [message queue](http://en.wikipedia.org/wiki/Message_queue) (RabbitMQ, ZeroMQ, ...), shared cache ([*memcached*](http://memcached.org/), ...), database, ... But those are probably way too much heavy weighted given your purpose.
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.