0

I'd like to set a specified path to $PATH,

I write it to the top of .bashrc

$ cat ~/.bashrc
export PATH=/usr/local/opt/coreutils/libexec/gnubin:$PATH

and activate it

$ source ~/.bashrc

It works

/usr/local/opt/coreutils/libexec/gnubin:/Users/me/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Users/me/.rvm/bin:/usr/local/mysql/bin:/Users/me/.rvm/bin:/Users/me/.rvm/bin:/Users/me/.rvm/bin

Nonetheless, when I open a new terminal, it's gone.

$ echo $PATH
/Users/me/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Users/me/.rvm/bin:/usr/local/mysql/bin

As a test, I closed all the terminals and restart fresh

$ echo $PATH
/Users/me/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Users/me/.rvm/bin:/usr/local/mysql/bin

The path /usr/local/opt/coreutils/libexec/gnubin is not there.

I checked my operations:

1. put desired path to the top
2. export it 
3. source to activate it.

What's wrong with my operations?

3

The Terminal app (as well as iTerm) on macOS (which I believe that you are using) starts a login shell by default. When bash is started as a login shell, it reads your ~/.bash_profile file, but not ~/.bashrc.

You may change the way that your terminal starts the shell in the terminal's preferences, or you may make your ~/.bash_profile source the ~/.bashrc file by adding the following to ~/.bash_profile (probably at the end of the file):

if [[ "$-" == *i* ]] && [ -f ~/.bashrc ]; then
    source ~/.bashrc
fi

This would make the login shell also read the ~/.bashrc file if it is an interactive shell and if ~/.bashrc exists.

As an alternative, you may obviously just add the modification to PATH in ~/.bash_profile instead. I believe macOS does not install a default ~/.bashrc file in users' home directories.

Related:

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.