Modify $PATH properly!

Discussion in 'Mac Programming' started by interslice, May 13, 2010.

  1. macrumors member

    interslice

    Joined:
    Jul 10, 2008
    Location:
    NL
    #1
    I have in my home directory:
    .bash_profile < this one has search strings (i recently modified this in text edit)
    export JAVA_HOME=/usr/bin
    export PATH=/usr/bin:$PATH


    .profile < this one tells me instructions about my profile
    .bash_login < this one is empty

    In Terminal in 2007 I changed my $PATH variable somehow to include "SoyLatte Java 1.6" for Mac Osx 1.4

    Now I upgraded to Mac 1.6 snow leaopard - and none of my Java works online

    As you can see my .bash profile = 'a normal basic setting'

    but when i type into Terminal 'echo $PATH' > i still get this:
    /usr/soylatte16-1.0.2/bin:/usr/soylatte16-1.0.2/bin:/bin:/sbin:/usr/bin:/usr/sbin

    and I can't change it!

    does anyone know how I can get back to my original settings - or how to make the $PATH reflect whats in my .bash profile?

    What exactly does the 'echo $PATH' read? it sure as hell doesn't read any of the .bash profile stuff or anything similar! and seems almost impossible to modify ... properly :confused:

    I also found this code snippet: http://codesnippets.joyent.com/posts/show/1800#related
    it looks really useful - does anyone know how to use it?

    Thanks
     
  2. macrumors 68020

    pilotError

    Joined:
    Apr 12, 2006
    Location:
    Long Island
    #2
    Did you do the setup in the .profile?

    .profile is executed first when sh, bash or ksh is started.

    .bash_profile/.bashrc only executes for bash specific shell commands.

    You should be able to echo $SHELL to make sure which shell your executing.

    you can also do an "ls -al | more" and take a look at all the hidden files that exist and the permissions on them. You need to own the file or have x access in order to run it.

    If these files don't exist in $HOME, you may have edited the /etc/profle or /etc/bashrc files, which are like global default files.
     
  3. macrumors 603

    Cromulent

    Joined:
    Oct 2, 2006
    Location:
    The Land of Hope and Glory
    #3
    It seems to me that you are missing underscores in your file names from what you put in your post.
     
  4. thread starter macrumors member

    interslice

    Joined:
    Jul 10, 2008
    Location:
    NL
    #4
    $ echo $SHELL = /bin/bash

    ls -al | more =
    -rw------- 1 chrisbrown chrisbrown 12445 May 13 18:29 .bash_history
    -rw-r--r-- 1 root chrisbrown 101 May 13 16:33 .bash_login
    -rw-r--r--@ 1 chrisbrown chrisbrown 132 May 13 14:59 .bash_profile
    -rw-r--r-- 1 root chrisbrown 12288 May 13 02:18 .bash_profile.swo
    -rw-r--r--@ 1 chrisbrown chrisbrown 799 May 13 13:06 .profile


    I don't know much about Mac Terminal editing ...
    I don't know if i did the setup in the .profile :confused:
    What actually physically changes when you edit the path variable?
    none of those files in the $HOME directory change!?

    I only ever executed the following commands in Terminal ...
    sudo vi .bash_login
    PATH=/usr/soylatte16-1.0.2/bin:$PATH


    This gave me the messy code after a few attempts:
    /usr/soylatte16-1.0.2/bin:/usr/soylatte16-1.0.2/bin:/bin:/sbin:/usr/bin:/usr/sbin

    I think I just need to delete the soylatte part so I can revert to the normal Java for SnowLeopard!

    Thanks for your help - i really appreciate it
     
  5. thread starter macrumors member

    interslice

    Joined:
    Jul 10, 2008
    Location:
    NL
    #5
    Yes it looks like i have editted the:
    etc - profile
    # System-wide .profile for sh(1)

    PATH="/usr/soylatte16-1.0.2/bin:/bin:/sbin:/usr/bin:/usr/sbin"
    export PATH=/usr/soylatte16-1.0.2/bin:$PATH

    if [ "${BASH-no}" != "no" ]; then
    [ -r /etc/bashrc ] && . /etc/bashrc
    fi


    can i change this in text edit instead?
     
  6. macrumors 68020

    pilotError

    Joined:
    Apr 12, 2006
    Location:
    Long Island
    #6
    I think you have to open the file with admin privs. sudo is easiest, but some text editors allow you to do it by recognizing the permissioning and asking for the password. I'm not sure text edit does that (never tried).
     
  7. thread starter macrumors member

    interslice

    Joined:
    Jul 10, 2008
    Location:
    NL
    #7
    I think the problem is fixed:
    This is really nice but i need a bit more help.

    Whenever i used to type ‘echo $PATH’
    only ever got back my old PATH – which i found impossible to change
    Until now!
    I created a .Profile file in my root directory (Mac:) ??
    I copied my text edited file **
    Now my echo $PATH reads perfect when I ask Terminal 'echo $PATH'

    My questions are:
    1.If you don’t have a .profile file in the correct place (what is the correct place? See Q2.)
    2.What is the root directory the .profile - .bash_login - .bash_profile files should be in? is it MAC or MAC/users/myname/

    3.What are the possible locations that terminal reads and writes to when you type in ‘echo $PATH’ and sudo vi .bash_login (to edit this path)
    OR what file is it reading to show you your $PATH!!! (because i was not able to change a file which reflected a change in $PATH in terminal
    (i have not found an answer to this question in 2 days of looking in forums)


    Now i have .profile files everywhere!!!
    I just have big problems understanding what Terminal is editing and showing when you edit the $PATH via the sudo vi .bash_login OR sudo vi .bash_profile (my files were never showing a universally similar PATH)

    Some people on several forums think that when you edit the $PATH variable via the sudo vi .bash_profile OR sudo vi .bash_login.
    OR where the $PATH is located is in the following places

    /usr/share/init/tcsh/login
    /etc/rc.common
    /etc/csh.cshrc
    /etc/profile
    /users/myname
    /

    Why is it so hard for me to understand what Terminal is REALLY showing when you type in echo $PATH
     
  8. macrumors 6502a

    Joined:
    Sep 17, 2007
    #8
    It is simply showing you the current value of the environment variable 'PATH'. That variable can be edited directly on the command line, a shell script you execute yourself or one that is automatically executed when you start up terminal (in more specific terms, when the bash shell is started). If you would like to know exactly what files Bash executes, when starting up, read the bash man page. It tells you in there which ones are read and in what order. If you use a shell other than bash (which you're not unless you changed it), then other files are read and can be found out by reading that shell's man page.

    It can also be changed via scripts that are executed when non-login shells are started but that's a more advanced topic (read about login vs non-login shells if you are curious).

    Code:
    man bash
    If you need to edit your path, only edit the files in your home directory unless you have a good reason to edit the system wide ones. Which one you choose to edit can depend on several factors. For your case, almost any of them will work but there are valid reasons to choose one vs the other (i.e. .profile vs .bash_profile vs .bashrc).

    Those files may or may not be read by other shells and/or on different operating systems. Again, the real answer as to which files are read can be found by reading the man page (on your actual computer).
     
  9. macrumors 68020

    pilotError

    Joined:
    Apr 12, 2006
    Location:
    Long Island
    #9
    When your account is created, you are assigned a home directory for that account. It is typically /Users/<account name>.

    You can see your accounts root directory by typing "echo $HOME"

    /Users/myname or whatever the $HOME variable equates to

    Re-read my post from above. Basically when you setup your account, you tell the system what your default shell is. In OS X, the default is bash and there really isn't any reason to change it.

    When you start terminal, the system knows you want a bash shell, so it actually launches the bash shell for you.

    You can verify this by typing in "ps" at the command prompt. You should see:


    PID TTY TIME CMD
    909 ttys000 0:00.01 login -pf myUserName
    913 ttys000 0:00.02 /bin/bash
    915 ttys000 0:00.00 ps


    Typically, when you modify a global profile, anybody who logs in will get what is contained in those files. Usually, admins put a very small set of things in those files (like the path to basic shell commands, etc).

    So every account on the node will get whatever is defined in

    /etc/profile

    anyone using bash will get what's contained in

    /etc/bash_profile
    /etc/bashrc

    Once the global definitions are executed, it looks in the $HOME variable to find the YOUR root directory and looks for certain files. If they exist, it executes them.

    $HOME/.profile - will get executed by sh, bash or ksh shells when they are started.
    $HOME/.bash_profile
    $HOME/.bashrc

    So since you edited both the global and the local .profile, you are seeing
    /usr/soylatte16-1.0.2/bin:/usr/soylatte16-1.0.2/bin:/bin:/sbin:/usr/bin:/usr/sbin

    You shouldn't really change the global version, but if it's your machine, it doesn't really matter.

    In response to your other question, when you make a change, you need to re-login for the change to take effect, or you need to re-execute what you just changed.

    So if you edited .profile, you can execute it in the current shell by:
    ". .profile" (Thats dot<space>.Profile)

    OK, here's what your missing. When bash reads the file, it is done with the file. When you define $PATH in the file, it copies it from the file into memory. You can look at all the definitions with the "set" command.

    So, when you echo $PATH, its looking at the memory version always.

    If you edit a file, you need to re-execute the file to copy it into memory.

    Sometimes you want the definition to remain only for the time this terminal window is open, so you can modify it at the command line and change only the memory version.

    ie.

    $ export PATH=$PATH:/someTemporaryDirectory

    when you close the terminal window, it will dissapear.

    So in short, if you define PATH in /etc/profile, everybody on the box gets it

    export PATH=/bin /usr/bin /sbin

    When you *Modify* it in /Users/myAccount, only you get it

    export PATH=$PATH /usr/soylatte16-1.0.2/bin

    when you echo $PATH, you will get

    /bin /usr/bin /sbin /usr/soylatte16-1.0.2/bin

    If you define the same thing in both places, you will get 2 copies of the same directory, which you found out when you modified both global and local profiles with the same PATH definition.
     

Share This Page