Meaning of `export PATH=…` in .bash_profile

18,815

The PATH before = is a variable name and the combination tells bash to store the stuff behind the = in the variable.
The $PATH is the value of the variable PATH up until then.

The combination PATH="some_path_to_stuff:$PATH" extends the PATH variable. In bash this is a colon (:) separated list.


Regarding the double addition of /usr/local/bin, I can only guess that the second version has no newline after it (and is at the end of the file). In principle this should give you a PATH which starts with /usr/local/bin:/usr/local/bin:..... You can check that with

echo $PATH

And if there is only one time /usr/local/bin then do:

echo "" >> ~/.bash_profile

and login an try to print $PATH again.

Share:
18,815

Related videos on Youtube

Shoeb
Author by

Shoeb

Updated on September 18, 2022

Comments

  • Shoeb
    Shoeb over 1 year

    I'm using a mac for some years now and as I'm currently learning ruby on rails, I felt like I should learn more about the system I'm using. I took a look at the ~/.bash_profile, it looks like this

    PATH="/Users/nikolajandersen/.apportable/SDK/bin:$PATH"
    
    [[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm" # Load RVM$
    
    # Setting PATH for Python 3.3
    # The orginal version is saved in .bash_profile.pysave
    PATH="/Library/Frameworks/Python.framework/Versions/3.3/bin:${PATH}"
    export PATH
    export PATH=/usr/local/bin:$PATH
    export PATH=/usr/local/bin:$PATH
    

    As there are two identical export PATH=/usr/local/bin:$PATH lines, I tried to delete the one. As a result, commands like nano and ls didn't work anymore. I reverted the change but I don't understand why it has to stated two times.

    Another question is: why does it sometimes $PATH and sometimes just PATH without the $? Why does -s "$HOME/.rvm/scripts/rvm" stand in [[ ]] braces? And what does export PATH do?

    Update This is the output of echo $PATH

    /usr/local/bin:/usr/local/bin:/Library/Frameworks/Python.framework/Versions/3.3/bin:/Users/nikolajandersen/.rvm/gems/ruby-2.0.0-p353/bin:/Users/nikolajandersen/.rvm/gems/ruby-2.0.0-p353@global/bin:/Users/nikolajandersen/.rvm/rubies/ruby-2.0.0-p353/bin:/Users/nikolajandersen/.rvm/bin:/Users/nikolajandersen/.apportable/SDK/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/opt/sm/bin:/opt/sm/pkg/active/bin:/opt/sm/pkg/active/sbin
    

    It seems like you were right with the newline. I deleted the line again and made sure that there is a blank line in the end. Now the commands are still working.

    • terdon
      terdon over 10 years
      Please don't combine multiple questions in a single post. The issue with PATH vs $PATH is explained here, and [[ ]] is a test operator, it basically means if. The duplicate exports should have no effect, that's weird, I recommend you focus on that one and make you question about the export.
    • Shoeb
      Shoeb over 10 years
      Ok, sorry for the multiple questions and thanks for the answers.
    • Bananguin
      Bananguin over 10 years
      Welcome to the bright side! Before we google, we like to read manuals, called man pages. Bash's man page (man bash) can answer all your questions.
    • terdon
      terdon over 10 years
      @Bananguin in the OP's defense, man bash is 5465 lines long, not the easiest of reads.
  • Shoeb
    Shoeb over 10 years
    Thanks for the additional info. I updated my question with the echo $PATH output. It looks a little weird.