|
A Ruby program may write to the ENV object, which on most systems
changes the values of the corresponding environment variables. However,
this change is local to the process that makes it and to any
subsequently spawned child processes.
This inheritance of environment variables is illustrated in the code
that follows. A subprocess changes an environment variable and this
change is seen in a process that it then starts. However, the change
is not visible to the original parent. (This just goes to prove that
parents never really know what their children are doing.)
puts "In parent, term = #{ENV['TERM']}"
fork do
puts "Start of child 1, term = #{ENV['TERM']}"
ENV['TERM'] = "ansi"
fork do
puts "Start of child 2, term = #{ENV['TERM']}"
end
Process.wait
puts "End of child 1, term = #{ENV['TERM']}"
end
Process.wait
puts "Back in parent, term = #{ENV['TERM']}"
|
produces:
In parent, term = xterm
Start of child 1, term = xterm
Start of child 2, term = ansi
End of child 1, term = ansi
Back in parent, term = xterm
|
|
|