Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

General Discussion

Console - Quiz - Environment variables are visible to child processes by default.

The Environment Variables quiz in the Console Deep Dive says that the statement "Environment variables are visible to child processes by default" is true:

However, in the Environment Variables video, from minutes 8 through 10, the teacher seems to be saying that environment variables are NOT visible to child processes. This was the sequence of inputs and outputs that showed this:

(input): MESSAGE="hello world"

(input): echo $MESSAGE

(output): hello world

(input opening up a new, child bash session): bash

(input): echo $MESSAGE

(output):

(the last output was blank, showing that the child bash process cannot see the MESSAGE variable that had been defined in the parent bash process.

I would appreciate any help to understand why the quiz answer is "True". Thanks in advance!

4 Answers

The answer is True because child processes of your current process (i.e. the same terminal window) will see this variable. For instance, if you had declared a variable as Message = "hello world", it is in the global scope. Any function or child of this process can access that variable. Opening up a new terminal window is considered a different process.

Now, if you declared a variable INSIDE a function, then any function or process outside of this function or process can't see or access this variable because it is scoped locally to the function. You can see the same principle in JavaScript or Ruby or Python or C.

Another example would be: ~ message75 = "hello world" ~ sample_app.py message75

in this example, you're passing message75 as an argument to this fictional program sample_app.py which can access anything inside message75. notice, just because you have sample_app.py in your current working directory, doesn't mean you can access the variables or functions inside this program.

I hope that helps.

I was also confused as first but after reading Tomasz's response I can put this in a picture:


bash 1, $MESSAGE is local variable to only this instance of bash program

bash 2, cannot access the value of $MESSAGE because it's local to bash 1 by default (unless you use export)


bash 1, $PATH is a global variable that all child process can access

bash 2, $PATH is a global variable that all child process can access


in other words, global variables such as the ones for environment (including PS1, PATH, USER, LANG) are accessible to child processes (including different instances of bash). The local declare variables such as $MESSAGE shown in the videos cannot be access by default

I'm a bit confused as well as I'm trying to see a way to explain why MESSAGE isn't accessible in the 2nd/child bash process in the same terminal window as its parent as shown by the blank upon entering "echo $MESSAGE".

Alfred> the video doesn't make a distinction between "user-defined" variables and environment variables. For example, $MESSAGE in the example is referred to as an environmental variable that cannot be made visible to child processes unless we modify it with the export function.

Any help would be much appreciated.

Bump.

This question really should be completely re-worded. It is very deceptive and probably causes more harm than good for new users trying to figure it out.


Both processes start their execution right after the system call fork(). Since both processes have identical but separate address spaces, those variables initialized before the fork() call have the same values in both address spaces. Since every process has its own address space, any modifications will be independent of the others. In other words, if the parent changes the value of its variable, the modification will only affect the variable in the parent process's address space. Other address spaces created by fork() calls will not be affected even though they have identical variable names.


Source: http://www.csl.mtu.edu/cs4411.ck/www/NOTES/process/fork/create.html