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

Development Tools Console Foundations Environment and Redirection Environment Variables

Why are changes to HOME visible to a daughter bash with out export?

Jim Hoskins clearly demonstrated that changes to PS1 or a newly create environment Variable (MESSAGE) are not passed on to a daughter process (new bash shell). The quiz reinforces this lesson with the question "Environment variables are visible to child processes by default." to which the accepted answer is False.

So please explain the following behavior when reseting the HOME variable (I got the same result on both the treehouse console and my Mac Terminal app)

treehouse ~ $ echo $HOME
/home/treehouse
treehouse ~ $ mkdir newhome
treehouse ~ $ HOME=/home/treehouse/newhome
treehouse ~ $ echo $HOME
/home/treehouse/newhome
treehouse ~ $ bash
treehouse@ip-10-170-97-102:/home/treehouse$ echo $HOME
/home/treehouse/newhome

Why wasn't export needed? What other environment variables are exempt from the 'local unless exported' rule?

2 Answers

Scott Murray
Scott Murray
8,353 Points

Um, did you just assume the child's gender?

Caleb Kleveter
MOD
Caleb Kleveter
Treehouse Moderator 37,862 Points

I would think it would be because of the "HOME=" in "HOME=/home/treehouse/newhome".

Caleb Kleveter Thank you for your answer. I am embarrassed to admit that I don't understand it, and I don't know if this is because something is obvious to you (and probably everyone else) that I am missing, or because I expressed my question so badly that it doesn't make sense.

Let me try to build on your answer to see if I can show my confusion. I interpret your answer to mean making the assignment (HOME=) should be enough for the new value to be passed down to a daughter process (bash). But compare the results with the code in my original post with the following in which I alter the prompt variable, PS1, instead of HOME:

treehouse:~/workspace$ echo $PS1                                                                 
\[\e]0;\u: \w\a\]\[\033[01;38;5;24m\]\u\[\033[00m\]:\[\033[01;38;5;26;34m\]\w\[\033[00m\]$       
treehouse:~/workspace$ PS1="newPrompt$"                                                          
newPrompt$echo $PS1                                                                              
newPrompt$                                                                                       
newPrompt$bash                                                                                   
treehouse:~/workspace$ echo $PS1                                                                 
\[\e]0;\u: \w\a\]\[\033[01;38;5;24m\]\u\[\033[00m\]:\[\033[01;38;5;26;34m\]\w\[\033[00m\]$

Note how PS1, unlike HOME, reverts to it's original state in the new bash shell. What makes the two environment variables behave differently?

cc: Jim Hoskins