$_ CLI In Depth
Learning progress 0%
lesson environment path dotfiles

export & How Children Inherit the Environment

5 min read Module 9: Environment, PATH & Dotfiles Updated Dec 5, 2026
Cheat Sheet
0:00 / 3:09

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.

BASH3 lines
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.

BASH2 lines
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.

Note

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.

BASH2 lines
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.

Shell variables, exports and exec

Visualizer

A variable stays private to the shell until it is exported. A child gets a copy of the exported set when it starts, and nothing a child changes comes back.

1 / 4
Shell memory
A1 (shell variable)
B2 (shell variable)
Environment block
B2 (exported)

export B copies B into the environment block. A stays only in the shell’s own memory. The environment block is what a program receives when it starts.

A=1; export B=2; bash -c 'echo "A=[$A] B=[$B]"'
Terminal lab · 2 steps · real Linux in your browser

export and what children inherit

Prove that a child process changes only its own copy, and that a prefix assignment reaches one command only.

01
A child cannot change its parent
Create child-changes.sh in /workspace. The parent exports STATE, a child changes its copy of STATE and moves to /tmp, and then the parent prints what it still has: ``bash #!/bin/bash export STATE=start bash -c 'export STATE=changed; cd /tmp; echo "child dir=$(pwd)"' echo "parent STATE=$STATE" echo "parent dir=$(pwd)" ` Run bash child-changes.sh. The parent still shows STATE=start and stays in /workspace`.
Show hint for step 1
The child receives a copy of the environment and its own working directory. Its changes stop when it exits.
02
Give one command a value
Create prefix-demo.sh in /workspace. It runs one child with MODE=fast as a prefix assignment, then prints the parent's value: ``bash #!/bin/bash MODE=fast bash -c 'echo "child MODE=$MODE"' echo "parent MODE=${MODE:-unset}" ` Run bash prefix-demo.sh. The child prints child MODE=fast and the parent prints parent MODE=unset`.
Show hint for step 2
A prefix assignment is visible only to the command that follows it.
Check yourself Question 1 of 4
Concept Check What a child receives recall

What does a child process get from its parent when it starts?

Recap & Key Takeaways

A child process gets a copy of the environment at exec time. Later changes in either process do not cross the boundary.
A directory change or a variable change inside a child process never affects the parent shell.
A prefix assignment gives one command a value for that run only. An export affects every command started afterward.
/proc/PID/environ shows the environment block a process was started with.
Was this lesson helpful? Spot an issue or have a suggestion? Let us know on GitHub.
Report an issue