A child process does not get your shell variables. export changes that. It puts the variable in the environment:
text
learner@tidepool:~$ COLOR=green
learner@tidepool:~$ export COLOR
learner@tidepool:~$ bash -c 'echo $COLOR'
greenOr set and export in one step:
text
learner@tidepool:~$ export COLOR=greenprintenv COLOR now lists it too.
| Shell variable | Environment variable | |
|---|---|---|
| Set with | NAME=value | export NAME=value |
echo $NAME in your shell | Yes | Yes |
In printenv | No | Yes |
| Child processes get it | No | Yes, a copy |
The copy goes one way
The child gets a copy. If the child changes its copy, the variable in your shell does not change. A child can never change the variables of its parent.
Your shellCOLOR=green
copy
ChildCOLOR=green, can change it to red
For one command only
Put the assignment before a command. The command gets the variable in its environment. Your shell does not keep it:
text
learner@tidepool:~$ GREETING=hello printenv GREETING
hello
learner@tidepool:~$ printenv GREETING
learner@tidepool:~$Programs often read settings from environment variables. This form gives a setting to one run of a program.