A process has an environment: name-and-value strings made available when it starts. The shell has its own variables, but an ordinary shell assignment does not automatically place a new variable in the environment passed to child programs. export marks a variable for that purpose.
The following uses a fresh shell to avoid depending on an existing variable. It uses Python 3 as a separate child process:
sh -c 'unset LESSON_COLOR; LESSON_COLOR=blue; python3 -c "import os; print(os.getenv(\"LESSON_COLOR\", \"missing\"))"; export LESSON_COLOR; python3 -c "import os; print(os.getenv(\"LESSON_COLOR\", \"missing\"))"'
The first child prints missing; after export the second prints blue. Each child receives a copy of the environment at startup. Changing it inside a child does not change its parent shell, nor does an export update processes that are already running.
For one command, an assignment before its name supplies a value to that command’s environment without changing the parent shell’s value:
LESSON_COLOR=green python3 -c 'import os; print(os.environ["LESSON_COLOR"])'
This prints green. unset LESSON_COLOR removes that name in the current shell if it was set; it cannot retract values already copied to children. Applications choose which environment names to read, and an environment variable is not an encrypted secret store: other processes or diagnostic tools may be able to inspect it depending on the system and permissions.
PATH is an environment variable used in command lookup. Its directory order and persistent configuration matter when installing software; we return to it in the next chapter.