Skip navigation
This discussion is archived

How to set the PATH in Terminal?

15807 Views 17 Replies Latest reply: Oct 3, 2010 6:17 PM by BobHarris RSS
1 2 Previous Next
SBR210 Calculating status...
Currently Being Moderated
Sep 14, 2010 3:58 PM
As a newbie to Bash and the Terminal, in connection with installing a new version of Ruby and various add-ons, I have been messing around with changing the PATH via various methods I've found on the internet, and trying to add a new directory to my PATH. Unfortunately, I cannot figure out how to set the PATH correctly. Here is what I understand so far:

1. Bash first looks to ".bash_profile" in the home directory for PATH settings.
2. If none, Bash then looks to ".bash_login" for PATH settings.
3. If none, Bash then looks to ".profile".
4. If none, Bash then looks to a system file in "/etc/profile" for default PATH settings.
5. For a new shell after login, Bash looks for a ".bashrc" file.

I cannot find any of the above files (I have Finder set to show hidden files already). Somehow, following some instructions from the internet without really knowing what I was doing over the past week I have set my PATH as follows:

/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/Users/*****/src:/usr/X11/bin

but I cannot find the file that is setting this PATH. I have tried creating a new ".bash_profile" shell file and putting it in various places such as "/bin" and "/usr/bin" but the file is not being read.

* As a temporary workaround I tried using the command "source .bash_profile" upon login to set the PATH using the .bash_profile file that I created, but it does not work.
* I am able to temporarily alter the path using the command "PATH=/usr/local/bin:$PATH" but this only lasts for an active shell; when I close out and reopen a new shell, it reverts to the default.

So my questions are:
Is it correct that there is a "/etc/profile" somewhere that is setting the default PATH? If so where is it? Should I alter it, or create a new ".bash_profile" somewhere to set the PATH? If not, where is my PATH being set? If I should create a new ".bash_profile", what exactly should go in it?

Thanks in advance for any assistance.
iMac, Mac OS X (10.6.4), Terminal 2.1.1
  • taylor.henderson Level 2 Level 2 (450 points)
    Currently Being Moderated
    Sep 14, 2010 4:18 PM (in response to SBR210)
    I think you are overthinking it.

    "set myPath to /Users/shortname/desktop/myFolder"

    should work.

    If you have reasons why that doesn't wrok please feel free to expand.
    macbook, Mac OS X (10.5.8), a white one
  • William Boyd, Jr. Level 6 Level 6 (10,475 points)
    Currently Being Moderated
    Sep 14, 2010 4:34 PM (in response to SBR210)
    SBR210 wrote:
    I cannot find any of the above files


    Welcome to Apple's discussion groups.

    Look for those files in Terminal with the command "ls -a". Most of them will be in your home directory. Others won't exist until you create them. For example, my system has only the file .profile .

    Somehow, following some instructions from the internet without really knowing what I was doing over the past week I have set my PATH as follows:

    /usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/Users/*****/src:/usr/X11/bin


    If you don't know how you did that, it's going to be difficult for someone else to figure it out for you. I'd start by checking your home directory for files modified around the time you tried that with the command "ls -l -a ~/".

    but I cannot find the file that is setting this PATH. I have tried creating a new ".bash_profile" shell file and putting it in various places such as "/bin" and "/usr/bin" but the file is not being read.


    As I mentioned above, that file has to be in your home directory.

    * I am able to temporarily alter the path using the command "PATH=/usr/local/bin:$PATH" but this only lasts for an active shell; when I close out and reopen a new shell, it reverts to the default.


    That's the way it works.

    Is it correct that there is a "/etc/profile" somewhere that is setting the default PATH?


    I wouldn't change that file. Make the change to a file in your home directory.

    If so where is it?


    The file name "/etc/profile" defines its own location. If that's a mystery to you, I recommend that you spend a lot more time studying Unix.

    Should I alter it, or create a new ".bash_profile" somewhere to set the PATH?


    The latter, in your home directory.

    where is my PATH being set?


    Apparently in whatever command file you modified. The first thing you should do is to try to identify that file.

    If I should create a new ".bash_profile", what exactly should go in it?


    If all you want that script to do is to set the PATH variable, it probably should contain a command to set the PATH and a command to export PATH, such as this:

    PATH=$PATH:/User/yourshortname/whateverelseyouwant
    export PATH
    MacBook 2.0 GHz, Mac OS X (10.6.4)
  • William Boyd, Jr. Level 6 Level 6 (10,475 points)
    Currently Being Moderated
    Sep 14, 2010 5:23 PM (in response to SBR210)
    SBR210 wrote:
    I just tried that with the command "set myPath to /usr/local/bin" but it seemed to have no effect when I checked "echo $PATH" after.


    That statement may make sense to AppleScript, but it doesn't match any Unix or bash syntax I know.
    MacBook 2.0 GHz, Mac OS X (10.6.4)
  • BobHarris Level 6 Level 6 (12,505 points)
    Currently Being Moderated
    Sep 14, 2010 7:30 PM (in response to SBR210)
    ... I created a new ".bash_profile" file directly under my home directory and it worked like a charm. Below is the content of the file:

    "PATH=/usr/local/bin:/bin/:/sbin/:/usr/bin/:/usr/sbin/:/Users/******/Ruby:/Users/*******/src:/Users/******/Java Programs/"

    What are the "..."'s? Is that just you, or is the contents of the file actually inside of double quotes?

    is there an 'export PATH' also in the file? As in

    PATH=/usr/local/bin:/bin/:/sbin/:/usr/bin/:/usr/sbin/:/Users/******/Ruby:/Users/*******/src:/Users/******/Java Programs/
    export PATH

    And did you either restart your Terminal session so your .bash_profile was read, or issue a 'source .bash_profile' command to execute the .bash_profile after your change?
    The only remaining problem is that from the home directory, I'm unable to directly run programs in the above-designated subdirectories (without first navigating to the subdirectory containing the file. Is there something wrong with the syntax or order of the directories I put in the path?

    For example: there is a file called "newprogram.rb" in the "Ruby" directory listed above. I should be able to execute it from the home directory using "ruby newprogram.rb" but it does not work. The same command works fine from within the "Ruby" directory.

    Does the 'ruby' command search PATH to find its scripts?

    Have you read the "man ruby" man page to see if it talks about PATH?

    A common Unix feature is that programs and scripts with the executable bit set, and are located in a PATH directory, will be run if you just type their names.

    For example, if the first line of newprogram.rb script starts with

    #!/usr/bin/ruby

    and if the script is executable

    chmod +x newprogram.rb

    And if it is in a directory in PATH, then just typing

    newprogram.rb

    should run your perl script using /usr/bin/ruby. If you want to use a different version of Ruby, then change the #! line so it point to your version of Ruby. Or if your version of Ruby is found earlier in PATH then the /usr/bin/ruby, you can use

    #!/usr/bin/env ruby

    as your script's first line which will search PATH for 'ruby' and use the first one it finds.

    Message was edited by: BobHarris
    MacBook/10.5.8, iMac/i7/10.6.2, PowerMac/G5/10.4.11(deceased), Mac mini/10.5.6, iPod Touch(2ndGen)/32GB/4.0.2, iBook/G4, MacBook Pro, iMac/Core2Duo
  • Tony T1 Level 6 Level 6 (8,125 points)
    Currently Being Moderated
    Sep 14, 2010 7:31 PM (in response to SBR210)
    This is new with Leopard:


    path_helper(8) BSD System Manager's Manual path_helper(8)

    NAME
    path_helper -- helper for constructing PATH environment variable

    SYNOPSIS
    path_helper [-c | -s]

    DESCRIPTION
    The path_helper utility reads the contents of the files in the directo-
    ries /etc/paths.d and /etc/manpaths.d and appends their contents to the
    PATH and MANPATH environment variables respectively. (The MANPATH envi-
    ronment variable will not be modified unless it is already set in the
    environment.)

    Files in these directories should contain one path element per line.

    Prior to reading these directories, default PATH and MANPATH values are
    obtained from the files /etc/paths and /etc/manpaths respectively.

    Options:

    -c Generate C-shell commands on stdout. This is the default if
    SHELL ends with "csh".

    -s Generate Bourne shell commands on stdout. This is the default if
    SHELL does not end with "csh".

    NOTE
    The path_helper utility should not be invoked directly. It is intended
    only for use by the shell profile.

    Mac OS X March 15, 2007 Mac OS X
    MacBook /, Mac OS X (10.6.4), / Windows xp (Boot Camp)
  • William Boyd, Jr. Level 6 Level 6 (10,475 points)
    Currently Being Moderated
    Sep 14, 2010 8:12 PM (in response to SBR210)
    SBR210 wrote:
    - I do not have an 'export PATH' - I don't really know what that is or what it does. Do I need that?


    Without that export statement the PATH definition is private to that script and disappears when the script ends. The export command propagates the definition of PATH to the "top level" of your Terminal environment. You can verify that your PATH definition is working by running the command "echo $PATH" and looking at the output. If you don't see the directories that you wanted to add, you're not done.

    I have not read the "man ruby" man page - I'm not familiar with that.


    You may find the "man" (manual) command very useful. For example, "man ruby" will show you the "manual" for ruby. A variant that can be useful is the "-k" (keyword) switch. For example, "man -k ruby" will list the "man" pages that are relevant to ruby.
    MacBook 2.0 GHz, Mac OS X (10.6.4)
  • Tony T1 Level 6 Level 6 (8,125 points)
    Currently Being Moderated
    Sep 15, 2010 8:49 AM (in response to SBR210)
    As William said, you forgot export PATH
    also, no need to repeat the existing PATH, its already in $PATH

    this should work:

    export PATH=$PATH:/Users/******/Ruby:/Users/*******/src:/Users/******/Java Programs/


    You can alternatively create the following text file (note blank last line), call it anything (i.e.mypath) and place it in directory /etc/paths.d

    /Users/*****/Ruby
    /Users/*****/src
    /Users/*****/Java Programs/

    MacBook /, Mac OS X (10.6.4), / Windows xp (Boot Camp)
  • rccharles Level 5 Level 5 (5,150 points)
    Currently Being Moderated
    Sep 15, 2010 7:48 PM (in response to SBR210)
    If you want to follow the path, leave off ruby & put in the #!

    my first ruby program.




    chmod 777 ~/hi.rb



    mac $ hi.rb
    hi hi... 
    mac $ ruby hi.rb
    ruby: No such file or directory -- hi.rb (LoadError)
    mac $ cat ~/hi.rb
    #!/usr/bin/env ruby
     
    print "hi"
    print " hi... "
    print "\r\n"
    mac $ 


    my version of ruby doesn't seem to support following the path for the program.

    mac $ pwd
    /Users/mac
    mac $ ruby hi.rb
    hi hi... 
    mac $ cat hi.rb 
    print "hi"
    print " hi... "
    print "\r\n"
    mac $ cd Desktop
    /Users/mac/Desktop
    mac $ ls -l /Users/mac/hi.rb
    -rw-r--r--   1 mac  staff  40 Sep 15 22:32 /Users/mac/hi.rb
    mac $ echo $PATH
    /sw/bin:/sw/sbin:/bin:/sbin:/usr/bin:/usr/sbin:/Developer/Tools:/usr/X11R6/bin:/ Users/mac
    mac $ ruby hi.rb
    ruby: No such file or directory -- hi.rb (LoadError)
    mac $ ruby ~/hi.rb
    hi hi... 
    mac $ ruby --version
    ruby 1.8.2 (2004-12-25) [powerpc-darwin8.0]
    mac $
    iMac G3 600, Mac OS X (10.4.11)
  • MrHoffman Level 6 Level 6 (11,695 points)
    Currently Being Moderated
    Sep 16, 2010 5:41 AM (in response to SBR210)
    For purposes of reference and not aiming at your profiles question...

    The [advanced bash scripting guide|http://tldp.org/LDP/abs/abs-guide.pdf] documents creating and maintain bash shell scripts, there is an [introduction to command line management|http://www.apple.com/server/macosx/resources/documentation.html] manual in the Apple Mac OS X Server documentation, and O'Reilly has a book or two (as do others) on this topic.

    There are some system- and command-level areas where there are divergences among the various families of platforms, the Mac OS X and Mac OS X Server platforms, the Linux flavors, and the various BSDs can have some differences.
  • douglas_goodall Level 1 Level 1 (0 points)
    Currently Being Moderated
    Oct 2, 2010 6:21 AM (in response to SBR210)
    After reading the postings, I found the syntax that seems to work.

    Place a file in your home directory called .bash_profile

    Put the following in it...

    PATH=$HOME/bin:$PATH
    export PATH
    MacBook Pro 17", Mac Pro 8-core, iPhone 3G-16, Mac OS X (10.6.4), Warning: I may be a fanboi (I love my Macs)
  • Tony T1 Level 6 Level 6 (8,125 points)
    Currently Being Moderated
    Oct 3, 2010 9:26 AM (in response to douglas_goodall)
    PATH=$HOME/bin:$PATH

    You have '/bin/' in your Home directory? Doesn't sound right.
    What do you have in the file /etc/path?
    should be:

    /usr/bin
    /bin
    /usr/sbin
    /sbin
    /usr/local/bin
    MacBook /, Mac OS X (10.6.4), / Windows xp (Boot Camp)
1 2 Previous Next

Actions

More Like This

  • Retrieving data ...

Bookmarked By (0)

Legend

  • This solved my question - 10 points
  • This helped me - 5 points
This site contains user submitted content, comments and opinions and is for informational purposes only. Apple disclaims any and all liability for the acts, omissions and conduct of any third parties in connection with or related to your use of the site. All postings and use of the content on this site are subject to the Apple Support Communities Terms of Use.