Paul Patterson
Paul Patterson

Reputation: 6918

Updating bash environment variables using source

On OS X Mountain Lion The source command only seems to update my path when I have added something to it in .bashrc or .bash_profile. If I delete a path from either of these files, then use source to update, the deleted path remains. An example...

Adding to my PATH in .bash_profile

In terminal

> echo $PATH
> "/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/opt/X11/bin"

Add to path in .bash_profile

export PATH=$PATH:~/Desktop

Back in terminal

> source .bash_profile
> echo $PATH
> "/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/opt/X11/bin:/Users/myname/Desktop"

So, all that went as expected; my Desktop has been added to my PATH. Now after I delete the previously added path from .bash_profile, leaving this file empty

> source .bash_profile
> echo $PATH
> "/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/opt/X11/bin:/Users/myname/Desktop"

As you can see the 'deleted' path '/Users/myname/Desktop' remains. Am I misunderstanding what source does? I thought It was equivalent to opening a new terminal window (which does return the result I was expecting - i.e. no Desktop path)

Upvotes: 1

Views: 2407

Answers (1)

Radu Rădeanu
Radu Rădeanu

Reputation: 2732

When you use source .bash_profile first time, because of export PATH=$PATH:~/Desktop line from .bash_profile file, your PATH is reassigned to old PATH to which is added ~/Desktop directory.

When you use source .bash_profile second time, the PATH is not anymore reassigned because you delete export PATH=$PATH:~/Desktop line. So, this time the value of your PATH remains unchanged (like before).

You have to restart your terminal (current shell) if you want that the value of your PATH to return to its initial value. Or you can source your /etc/environment file:

source /etc/environment

Upvotes: 1

Related Questions