Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

How to set the PATH in Terminal?

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

Posted on Sep 14, 2010 3:58 PM

Reply
17 replies

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

Sep 14, 2010 5:39 PM in response to William-Boyd-Jr

@ William Boyd, Jr.
Thanks for the response. 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/"

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.

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

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

Sep 14, 2010 7:58 PM in response to BobHarris

@BobHarris
Thanks for the response. To answer your questions:
- The quotes are just in this post; I didn't have them in the actual file.
- I do not have an 'export PATH' - I don't really know what that is or what it does. Do I need that?
- I did restart Terminal each time.
- The "ruby" command is supposed to search each directory in the PATH for the files it is told to execute.
- I have not read the "man ruby" man page - I'm not familiar with that.
- The Unix feature you mentioned, which I learned as "shebang", is working. When I have that line at the beginning of the program, I can run the program directly by typing in it's name from the home directory or the actual directory containing the file. The problem is, for programs without that line, I have to be in the actual directory to run the file. I should be able to enter the command "ruby example1.rb" from my home directory and execute the file from there, since the subdirectory is (or should be) now in my PATH. Both the containing folder ("Ruby") and the file "example1.rb" have user RWX access set as permissions. Does the fact that I can shebang the files from the home directory mean that the PATH setting is functioning correctly?
Here is the error message I get when trying to run a file in the "Ruby" subdirectory from the home directory:

iMac:~$ ruby newprogram.rb
ruby: No such file or directory -- newprogram.rb (LoadError)

Thanks again for the help.

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.

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/

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 $

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.

How to set the PATH in Terminal?

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.