What does export do in BASH? [duplicate]
Possible Duplicate:
Difference between “a=b” and “export a=b” in bash
It is hard to admit, but I have never really understood what exactly export
does to an environment variable. I know that if I don't export a variable I sometimes can't see it in child processes, but sometimes it seems like I can. What is really going on when I say
export foo=5
and when should I not export a variable?
From man bash
:
ENVIRONMENT
When a program is invoked it is given an array of strings called the environment. This is a list of name-value pairs, of the form name=value.
The shell provides several ways to manipulate the environment. On invocation, the shell scans its own environment and creates a parameter for each name found, automatically marking it for export to child processes. Executed commands inherit the environment. The
export
anddeclare -x
commands allow parameters and functions to be added to and deleted from the environment. If the value of a parameter in the environment is modified, the new value becomes part of the environment, replacing the old. The environment inherited by any executed command consists of the shell's initial environment, whose values may be modified in the shell, less any pairs removed by theunset
command, plus any additions via the export anddeclare -x
commands.
Exported variables get passed on to child processes, not-exported variables do not.
When you use export
, you are adding the variable to the environment variables list of the shell in which the export command was called and all the environment variables of a shell are passed to the child processes, thats why you can use it.
When you finish the shell its environment is destroyed, thats why the environment variables are declared and exported at login, in the .bashrc file for example