4

I'm trying to figure out how I can load changes to my .bash_profile without having to logout of my Bash session or exit Terminal, also without affecting my $PATH.

In a new session this is my $PATH:

/Users/MyName/.rbenv/shims:/usr/local/heroku/bin:/usr/local/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/X11/bin:/usr/local/git/bin:/usr/local/go/bin

And this is the alias I setup to reload my .bash_profile:

alias reload='source ~/.bash_profile'

When I run the reload command, my changes to my .bash_profile are loaded, but my $PATH is lengthened with each time I run the command.

For example:

>> reload

>> echo $PATH

/Users/MyName/.rbenv/shims:/usr/local/heroku/bin:/usr/local/bin:/Users/MyName/.rbenv/shims:/usr/local/heroku/bin:/usr/local/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/X11/bin:/usr/local/git/bin:/usr/local/go/bin

As you can see my $PATH is now longer and I think this is because the following is in my .bash_profile:

export PATH="/usr/local/bin:$PATH"
export PATH="/usr/local/heroku/bin:$PATH"

How can I keep my $PATH from being updated with each reload?

garythegoat
  • 1,517
  • 1
  • 12
  • 25

3 Answers3

8

Okay, trick number one: You reload a script like .bash_profile using

$ source .bash_profile

The source built-in reads a shell script as if it were coming from standard input, instead of forking a sub shell.

Trick number two: That assignment

PATH=/my/new/path:$PATH

is just appending what was already in PATH to the new string. So, it's like doing

PATH=/my/new/path:my/old/path/:/my/even/older/path

You don't like that behavior, you just reset PATH first. What I do is like this:

PATH=
PATH=/bin:$PATH
PATH=/usr/bin:$PATH

and so on. Now the path will be exactly what you expect.

Update

Here's exactly how my file looks:

export PATH=
export PATH=/bin
export PATH=${PATH}:/sbin
export PATH=${PATH}:/usr/local/bin
export PATH=${PATH}:/usr/sbin
export PATH=${PATH}:/usr/bin
export PATH=${PATH}:/usr/local/opt/go/libexec/bin

the exports aren't really important, as PATH is already exported, they're just me being fastidious.

You can also look at this answer for some of the rest of how I set up my shell: About .bash_profile, .bashrc, and where should alias be written in?

You should look at the Bash Guide for Beginners, there's lots of useful stuff there.

Community
  • 1
  • 1
Charlie Martin
  • 110,348
  • 25
  • 193
  • 263
  • Hi Charlie, I am receiving this error now after adding `PATH=` to my `.bash_profile`: `env: bash: No such file or directory` and none of my usual commands work. Do you know what I'm doing wrong? – garythegoat Jan 25 '15 at 19:56
  • Yeah, you need to follow it by setting the components of your path. That message is telling you that you've got nothing in your path. – Charlie Martin Jan 25 '15 at 23:49
  • Thanks for your answer Charlie. I believe I need to find the file in `.rbenv` that is first adding to my `$PATH` and add the `PATH=` there because I was receiving the error above even when my `$PATH` was subsequently being written to, even though I had the following in my `.bash_profile`: `PATH=`, `export PATH="/usr/local/bin:$PATH"`, `export PATH="/usr/local/heroku/bin:$PATH"`, and still: `env: bash: No such file or directory` – garythegoat Jan 26 '15 at 00:09
2

Or, you can check before inserting or appending to PATH if the value is not there already:

insert_to_path()
{
    if [[ "$PATH" =~ (^|:)"${1}"(:|$) ]]
    then
        return 0
    fi
    export PATH=${1}:$PATH
}

insert_to_path /usr/local/bin
insert_to_path /usr/local/heroku/bin
Diego Torres Milano
  • 65,697
  • 9
  • 111
  • 134
0

Simplest way I can think of, change your alias so that it stores the existing path and resets it after sourcing the file:

alias reload='tmp=$PATH;source ~/.bash_profile;export PATH=$tmp'
Squirrel
  • 2,262
  • 2
  • 18
  • 29