Fork, exec, and the copy
When bash runs an external command, it creates a new process by duplicating itself and then replaces the copy with the new program. The duplication step is fork, and the replacement step is execve. The environment is handed over during the replacement. Bash builds an array of NAME=value strings from its exported variables and passes that array to execve along with the program path and the arguments. The new program receives only that array.
Because of this order, the environment a program sees is fixed when the program starts. Bash can change its own variables at any moment, but the child already has its own copy. The child is not connected to the parent by a live link.
A child can read but not write back
The next example shows the rule in both directions. The inner bash changes the variable and the directory, and the outer shell keeps its original values.
export COLOR=blue
bash -c 'echo "child sees $COLOR"; COLOR=red; cd /tmp; echo "child now $COLOR in $(pwd)"'
echo "parent still $COLOR in $(pwd)"The output is three lines. The child first sees blue, then changes its own copy to red and moves to /tmp. The parent then prints blue and the directory it was already in. The child's changes disappear when the child exits. This is the same reason that a script you run with bash script.sh cannot change the directory of your terminal. The working directory is per process, and the child gets its own copy of it.
Other process state behaves the same way. File descriptors, the umask, and signal handling are inherited as copies, and later changes in one process do not affect the other. The environment is simply the most visible of these copies, because you can inspect it with ordinary commands.
Setting a value for one command
A prefix assignment passes a value only to the program that follows it. The parent never sees the value. You can use this to give a program a setting without exporting it in your shell.
MODE=fast bash -c 'echo "child MODE=$MODE"'
echo "parent MODE=${MODE:-unset}"The first line prints child MODE=fast. The second prints parent MODE=unset. The prefix form and the export form differ in one important way: the prefix form applies to a single command, while an exported variable applies to every command that the shell starts afterward.
Cleaning the environment
Sometimes you want a program to run with almost nothing from your shell. The env command can start a program with an empty environment when you use -i, and it can remove one variable with -u. Bash can do the same for a single exec: exec -c replaces the shell with the program and passes an empty environment. Clearing the environment is a common step in reproducing a bug, because it shows whether a program depends on something in your shell that you did not know about.
An empty environment still leaves the program with the default behavior of the C library. Many programs then fall back to a default PATH or a default locale, so a clean run is not the same as a run on a machine with no software at all.
Looking at the block the kernel received
Linux stores the environment that a process started with in its /proc entry. The file /proc/PID/environ holds the strings separated by null bytes, so you translate the null bytes into newlines before you read them. The shell variable $$ is the PID of the current bash process.
export COLOR=blue
bash -c "tr '\\0' '\\n' < /proc/\$\$/environ | grep ^COLOR="The output is COLOR=blue. The child bash was started after the export, so its block contains the value. The variable $$ is expanded inside the child, so it names the child process. If you export a variable in a shell that is already running, that shell's own block does not change, and the value appears only in programs started after the export. Bash does not rewrite the block as its variables change.
Why scripts cannot change your shell
This is the most frequent surprise for new users. You write a script that sets a variable and changes the directory, run it with bash or by path, and find nothing changed when it finishes. The script ran in a child process, so its changes lived in the child copy and disappeared with it. The fix is not to make the child change the parent, because the design does not allow that. The fix is to run the file inside the current shell with the source command, which reads its lines as if you had typed them at the prompt.
A useful rule is that anything you would have to type again in every new shell belongs in a file, and anything you type once to test an idea belongs at the prompt. Mixing the two is what turns a startup file into a pile of half-remembered experiments.
Sourcing has a cost. A sourced file can change anything in your shell, including PATH, aliases, and options, so a mistake in it affects the whole session. Use sourcing for configuration files that are meant to change the shell, and run ordinary tasks as child programs so that their side effects stay contained.
A practical rule
When you need a value in a program, export it before you start the program, and keep the export in the same place that starts the program. When you need a value for one run, use a prefix assignment. When you need a change to come back to the shell, a child process cannot do it for you. Use source or . to run the file inside the current shell instead, which the startup file lesson covers in detail.