Global variables live from the moment they're set up until the moment they're unset or the process ends. Global (nor local) variables are not inherited by unrelated child processes (if the child process is a fork (a subshell) then it gets a copy of everything -- exported or nonexported).
Global exported variables are like global variables, but they're also automatically inherited (as part of the process environment) by, even unrelated, child processes. (With exports, you can pass a variable such as CXXFLAGS
to a process such as make
, which most definitely isn't a subshell of your shell, and every process that that make
spawns will also get that variable too.)
In your example ()
creates a subshell, which gets a copy of everything.
The foo
command modifies the subshell by adding an exported variable and then the subshell ends without ever utilizing the exported variable (no grandchild inheritted it). Now, no information implicitly travels from child processes to parent processes. What children do in their environment has no affect on their parents. That is the reason why your variable one
is unset.
BTW, those eval
s are an unnecessary eval
in this context.