Environment Variables

Environment variables are the cause of much confusion. People sometimes think of "the environment" as a global system-wide pool of settings that processes dip into (like the "registry" in Windows). This is incorrect.

To summarize:

Environment variables (shown with env) are not the same thing as Bash Parameters (shown with set).

They are not global. Every process has its own set.

They are only copied from the parent when the child process is created.

You cannot update another process's variables (not even if they are your children or your parent). You can only modify your own env vars.

What is the "environment"

The environment is an area of memory for each and every process. When the process creates a new process (forks), the fork's environment is generally copied from the old process' environment. As a result, the new process has an environment that is identical to, but a copy of, the original process.

What this means to us Bash users, is that whenever we start a new process from our bash scripts, these processes will inherit our script's environment. It's important to know that we're talking about a copy here, and that this copying only ever happens during the creation of the new process.

The same process applies not only to processes started from your Bash scripts, but any processes started on your system: Each has inherited the environment of their parent process; added to, modified or removed stuff from their copy of it, and whatever children they spawned off themselves have inherited that modified environment.

That makes no sense. Illustrate!

First, let's make sure that the Bash parameters MY_ENV_VAR and myParameter do not already exist. Assuming the parent of the current shell didn't have said variables in their environment and you did not create them yet, this step is not really necessary (doesn't hurt though). In our output, we'll use the tag "parent" to mark the current shell's output, and the tag "child" to mark output generated by a child process.

Now, let's create some variables. These commands will create two Bash parameters called MY_ENV_VAR and myParameter, and then linkMY_ENV_VAR to Bash's environment, causing it to create an env var of the same name with matching content. We will notexport myParameter, so that one exists purely as a Bash parameter. The declare -p builtin command shows this.

Here's how we would update our env var. We basically update the Bash parameters. The parameter that's linked to the environment will cause Bash to update its env var of the same name as well (if a variable is already exported, you don't even really need export again).

Note that these echo statements are expanding our Bash parameters, not environment variables. That's why we see both expansions resulting in "Bye" even though only MY_ENV_VAR is linked to an environment variable.

Let's demonstrate the effects of the environment now. As I said, when we create a new process, we copy the environment into the new process' environment. The environment holds only MY_ENV_VAR, not myParameter (since we only exported the former). Here, we create a new Bash process that runs some bash code which will expand our parameters again. The new Bash process has noticed its environment contains an environment variable named MY_ENV_VAR, so it has created a Bash parameter with the same name. Since there is no myParameter on the environment, it hasn't created this parameter, and it expands empty.

Our child has only MY_ENV_VAR but back in our shell, we still have both parameters.

Let's demonstrate that a child Bash process' version of MY_ENV_VAR is not the same as the shell's version of it. Here, we modify MY_ENV_VAR in the child, which will update the child's environment variable of the same name. But when we look at the shell's environment variable, we still find the old value: The child can update its own environment, but that does not affect the parent's copy.

Let's try a trick now to demonstrate the reverse: Updating MY_ENV_VAR in the parent also doesn't update the child'sMY_ENV_VAR. Here, we'll start a child process that shows its own version of MY_ENV_VAR, waits a little while, and then shows it again. While the child is waiting, we'll update MY_ENV_VAR in our shell's copy. This change will not affect our child's second output of MY_ENV_VAR, since the child's version of it is a copy at the time of the child's creation and any changes to it after it was copied are not carried over.