Why does changing $PATH affect child shells, but changing $foo not?
There are really two types of variable:
- Environment variables
- Shell variables
To make things more complicated, they both look the same, and a shell variable can be converted to an environment variable with the export
command.
The env
command will show the current set of environment variables.
$ myvar=100
$ env | grep myvar
$ export myvar
$ env | grep myvar
myvar=100
Variables can also be temporarily exported for the life of a command.
$ env | grep anothervar
$ anothervar=100 env | grep anothervar
anothervar=100
$ env | grep anothervar
$
When the shell starts up it inherits a number of environment variables (which may be zero).
Startup scripts (eg .bash_profile
, .bashrc
, files in the /etc
directory) can also set and export variables.
Finally the shell, itself, may set a default number to environment variables if the environment is empty. e.g.
$ PATH=foo /bin/bash -c 'echo $PATH'
foo
$ PATH= /bin/bash -c 'echo $PATH'
$ unset PATH
$ /bin/bash -c 'echo $PATH'
/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/bin:/sbin:.
I recommend you read this article.
There is a big difference between shell and environment variables. PATH is one of environment ones, and is already exported (by default), so by changing it, the current shell your using and all the others - any child shells or processes - will be affected.
PATH is list of directories that the system will check when looking for commands. So even if you wanna change it or add something to it, it's better to use PATH="$PATH:...
since you need to have the original PATH for executing commands, but if you alter this completely, you should use your commands by entering their absolute path, for instance /bin/pwd
.
But foo which you're exporting is not something having any effect on shell or processes, I know you're trying to making it a environment variable, but you haven't defined something effective yet so that's why you cannot see what effect it can have on other things, in fact it's what you're defining because of you own needs at the moment, i.e. to keep track of some ephemeral data, so it'd be better to keep it just a shell variable. For instance, when you're trying to do some mathematical projects, you might be in need of defining something and have them for a short time, so you can set just some shell variables without needing to export them.
I hope I could be precise, but I still recommend you read the given article, and play around with changing some of those variables so you'll fully understand. BTW, if you want a environment variable to be permanent, you should add them as a line in your .bashrc
or .bash_profile
.