3

I want to use the latest version of git which I installed via Homebrew, so I needed to change my PATH variable to ensure OS X uses the Homebrew version instead of the outdated version that ships with OS X and Command Line Tools. This tutorial recommends simply running:

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

That's what I did and it worked great.

My question is: where did this command modify my PATH variable? I don't have ~/.bashrc, ~/.bash_profile, or ~/.profile files in my home directory at all, but when I $ echo $PATH then the PATH appears correct and when running which git and git --version everything appears to point to the right place. I do see the correct paths listed in /etc/paths but if I add a test path using export from the command line then the test path does not appear in that file.

Another question: is this a good way to modify my PATH variable?

Final question: what are the advantages and disadvantages of this method?

Edit: I do see the correct paths listed in /etc/paths but if I add a test path using $ export PATH=/test:$PATH then the test path does not appear in /etc/paths/, but it appears first when I run $ echo $PATH, however it is lost after rebooting.

  • This is a duplicate of many (and you have several questions here) - however /usr/local/bin is on your default path set in /etc/paths – user151019 May 27 '15 at 15:51
  • 1
    @Mark I believe my main question is more specific than most other questions I've seen on the topic. I have not yet seen a question where it is answered. So telling me that it's a duplicate is essentially saying "someone somewhere answered your question" but omitting where the answer may be within a vast body of questions and answers. If you care to point me in the right direction then that would be most helpful. If it is indeed answered elsewhere then I will happily remove my question. Thank you. – cfx May 27 '15 at 16:15
  • Sorry I eft the second bit of the comment as the answer until I could find a decent duplicate and found one but forgot to give it see apple.stackexchange.com/a/121021/237 - and see what the answer lists in /etc/path. Also please comment on what is not explained on that answer as we need to get a canonical answer set up - so the next person does not have to do all the searching – user151019 May 27 '15 at 16:22
  • re the adding to /etc/paths not working - that I think should be a separate question as it does work so we need to know exact try what you did (including if yo did a reboot which is needed) – user151019 May 27 '15 at 16:25
  • @Mark thanks for clarifying. I will consult that stack and perform some more tests and update this stack according to what I uncover. – cfx May 27 '15 at 16:47
3

On June 3 you posted the following comment

@DavidAnderson So if I understand correctly, running export PATH... from an interactive shell session only affects $PATH for that session and the newly added path is not saved anywhere (and therefore there is no file where the newly added path can be edited). E.g., if I run export PATH=/test:$PATH but I meant to export the /testing_123 dir instead, then my best option is to simply start a new session rather than attempt to locate and edit a file containing the erroneous /test dir (because there is no such file). Correct?

My response to your comment

From the manual page for bash, I have extracted the following.

A parameter is an entity that stores values.  It can be a name, a
number, or one of the special characters listed below under Special
Parameters.  A variable is a parameter denoted by a name.  A variable 
has a value and zero or more attributes.

PATH is the name of a variable. Normally, this variable has only the export attribute turned on. The export attribute marks a variable for export to subsequent commands via the environment. The export command turns on the export attribute, but in the case of the PATH variable, the export attribute is already turned on. Therefore, for the PATH variable, the next four commands are equivalent.

PATH=/test:$PATH
export PATH=/test:$PATH
declare PATH=/test:$PATH
typeset PATH=/test:$PATH

As for your comment, you are correct. There is no such file.

In the case of a mistake of the type you mentioned, I do not think starting a new session would be the best option. I would enter the command

declare -p PATH

and then highlight the output. You can then cut and paste the text as your next command. Before hitting the return key, use the other keys to correct your mistake.

For more information on this subject, see the manual page for bash. Either enter the command

man bash

or visit the site: bash - GNU Bourne-Again SHell.

In response to your Edit:

Yes, you are suppose to lose /test in your PATH variable when you reboot. This is why you need to put an export command in your ~./profile file.

Original answer:

You seem confused when the ~/.bashrc, ~/.bash_profile, and ~/.profile files execute. Maybe this will help.

Here is how I set up my computer. I have three files: ~/.profile, ~/.bashrc and ~/.bashcm. If you are missing any of these, you can create them.

When I start the Terminal application or select "New Window" from "Shell" on the menu bar, an interactive bash login shell is invoked. This shell executes the ~/.profile script at startup. You can substitute ~/.bash_profile or ~/.bash_login for ~./profile if you wish. If an interactive bash shell that is not a login shell is started, the shell reads and executes commands from ~/.bashrc. The purpose of ~/.bashcm is to provide a common script to be executed by both ~/.profile and ~/.bashrc.

Here is an example of a .profile file

echo in .profile
source ~/.bashcm
export PATH=$PATH:~/bin
echo exit .profile

The purpose of the echo commands are to illustrate when the scripts are executed. Once you are comfortable, you can either comment out or delete them. The .profile file executes .bashcm script, then modifies the PATH variable and exits. An example .bashcm file is shown below.

echo in bashcm
PS1='\h:\W \u\$ '
alias home='cd ~'
echo exit .bashcm

Other than the echo commands, .bashcm only sets the prompt and an alias. The example .bashrc file shown below simply executes the .bashcm script. Again the echo commands are just for illustration purposes.

echo in .bashrc
source ~/.bashcm
echo exit .bashrc

If I start the Terminal application and enter the commands

echo $PATH
alias
bash
echo $PATH
alias

in the window, I get the following.

Last login: Wed May 27 14:48:19 on ttys000
in .profile
in bashcm
exit .bashcm
exit .profile
Steelhead:~ davidanderson$ echo $PATH
/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Users/davidanderson/bin
Steelhead:~ davidanderson$ alias
alias home='cd ~'
Steelhead:~ davidanderson$ bash
in .bashrc
in bashcm
exit .bashcm
exit .bashrc
Steelhead:~ davidanderson$ echo $PATH
/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Users/davidanderson/bin
Steelhead:~ davidanderson$ alias
alias home='cd ~'
Steelhead:~ davidanderson$ 
  • export actually makes sure that PATH is visible in processes run from within the shell. Without export, variables are only visible in the current (shell) process, not in any child processes. – nohillside May 27 '15 at 21:16
  • 1
    @DavidAnderson So if I understand correctly, running export PATH... from an interactive shell session only affects $PATH for that session and the newly added path is not saved anywhere (and therefore there is no file where the newly added path can be edited). E.g., if I run export PATH=/test:$PATH but I meant to export the /testing_123 dir instead, then my best option is to simply start a new session rather than attempt to locate and edit a file containing the erroneous /test dir (because there is no such file). Correct? – cfx Jun 3 '15 at 15:11
  • @patrix: Upon further research, it turns out the use of export is optional for the PATH variable. So for example, PATH=$PATH:~/bin would be equivalent to export PATH=$PATH:~/bin. To make the two commands act differently, you would first have to enter the declare +x PATH command. This would turn off the export attribute. – David Anderson Jun 4 '15 at 16:15
  • 1
    @cfx: I updated my answer in response to your latest comment. – David Anderson Jun 4 '15 at 16:18
  • It's enough to export a variable once, for PATH this already takes place in /etc/profile when the output of /usr/libexec/path_helper -s is evaluated. – nohillside Jun 4 '15 at 17:27

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .