Want to highlight a helpful answer? Upvote!

Did someone help you, or did an answer or User Tip resolve your issue? Upvote by selecting the upvote arrow. Your feedback helps others! Learn more about when to upvote >

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

Adding something to my $PATH permanently

Hi folks. I want to add /usr/local/bin to my $PATH, permanently. After reading the threads in here, I still am confused about how to do it.

This is what I am trying:

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

So when I do this:

echo $PATH

...it comes back fine. Then I close the window, she goes back to the normal value without /usr/local/bin.

Any ideas? I'm running the bash shell.

Cheers

G5 1.8/Mini 1.67/Black MacBook/G3 300/PB180c/Mac SE/iMacs & blah blah blah, Mac OS X (10.4.10), iPhone? Heck I'm still giggly about MultiFinder!

Posted on Jun 26, 2007 4:57 PM

Reply
9 replies

Jun 26, 2007 5:18 PM in response to BioRich

Hi folks. I want to add /usr/local/bin to my $PATH,
permanently. After reading the threads in here, I
still am confused about how to do it.

This is what I am trying:

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

So when I do this:

echo $PATH

...it comes back fine. Then I close the window, she
goes back to the normal value without
/usr/local/bin.

Any ideas? I'm running the bash shell.


To set this value permanently, add the PATH=... and export PATH commands to the .bashrc file in your home directory. If .bashrc doesn't exist, then create it. The .bashrc file is read every time you start a new shell, so any "permanent" modifications should go there.

MacBook C2D (home), PowerMac Dual G5 (work) Mac OS X (10.4.10)

Jun 26, 2007 7:11 PM in response to BioRich

&> Hi guys.
I touched a file called .bashrc in my home directory.
It now has the following value in it:

PATH="/usr/local/bin:/bin:/sbin:/usr/bin:/usr/sbin:usr
/local/mysql/bin"

When I create a new terminal, I echo $PATH and it
isn't in there.


Two things:

1) You probably want to set PATH= <new directories...>:$PATH. Otherwise, you'll lose your existing PATH.

2) You need the line "export PATH" following the "PATH=..." statement. (Or you can do it all on one line, as Mark suggested.)

MacBook C2D (home), PowerMac Dual G5 (work) Mac OS X (10.4.10)

Jun 26, 2007 7:37 PM in response to Asher_Langton

Sorry for the confusion, but I'm totally confused.

What do I do? I tried the single statement with "echo" and the word "export" showed up in my .bashrc file for some reason.

I've never done any of this before, and I've been given 3 files to create, with 2 ways of exporting to such a file. Can we focus on one way of doing things, to make it clear?

Thanks

Jun 27, 2007 12:38 AM in response to BioRich

Sorry for the confusion, but I'm totally confused.

What do I do? I tried the single statement with
"echo" and the word "export" showed up in my .bashrc
file for some reason.

I've never done any of this before, and I've been
given 3 files to create, with 2 ways of exporting to
such a file. Can we focus on one way of doing
things, to make it clear?


When a bash shell starts up, it reads several startup files: .bashrc, .bash_profile, and possibly .profile and .login. (I have no idea why there are so many options; personally, I use the /bin/tcsh shell and put all of my startup command in a .cshrc file.)

In any case, you should be fine putting these commands in either .bashrc or .bash_profile. First, let me break down the echo command that was recommended to you:

echo "export PATH=\"/usr/local/bin:$PATH\"" > .bash_profile

First, as you probably guessed, echo just repeats its argument, evaluating any shell variables. If you had just typed echo "export PATH=\"/usr/local/bin:$PATH\"", then the quoted string would have just been printed out to the terminal. However, the '>' is the redirect operator, and it sends the output from a command to a file, in this case .bash_profile. (One thing to note is that using '>' will overwrite an existing file; it's often better to use the '>>' operator instead, which just appends the output to the end of the existing file.)

Back to solving your problem: open up .bash_profile in a text editor. (If you're using TextEdit, you can do this at the command line with "open -a /Applications/TextEdit.app ~/.bash_profile".) Add the line:

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

to the file. Save the file and open up a new terminal, then type "echo $PATH" to see if it worked correctly.


MacBook C2D (home), PowerMac Dual G5 (work) Mac OS X (10.4.10)

Jun 27, 2007 2:38 AM in response to BioRich

Hi BioRich,
If you read the invocation section of the bash man page you will find the following:

When bash is invoked as an interactive login shell, or as a non-inter- active shell with the --login option, it first reads and executes com- mands from the file /etc/profile, if that file exists. After read- ing that file, it looks for ~/.bash_profile, ~/.bash_login, and ~/.pro- file, in that order, and reads and executes commands from the first one that exists and is readable. ... When an interactive shell that is not a login shell is started, bash reads and executes commands from ~/.bashrc, if that file exists.

What the man page doesn't tell you is what a login shell is. It is generally the first shell you open and it usually suffices to set the PATH in that shell because subsequent shells opened in the same terminal application will inherit the environment of the login shell. However, shells opened in other terminal applications will not inherit the environment. The bash shell fails to consider this now common situation.

To get around this shortcoming, it is common to put all of your configuration commands in a file sourced by a login shell, such as the ~/.bash_profile as suggested by Asher, and then to source that file from the ~/.bashrc.

That gives rise to a new problem: redundant path elements. If a non-login shell does inherit the environment of the login shell then the sourcing the ~/.bash_profile will add path elements a second time. A solution to this problem is to test the environment before sourcing the ~/.bash_profile to see if the environment has already been "fixed." A slightly more elegant solution is to check the path before adding to it. This can be done with functions like "append_path" or "prepend_path" functions like those defined in FInk's init.sh file.

Edit: They stopped respecting the <BLOCKQUOTE> tag in the stupid forum software! This just gets worse and worse.
--
Gary
~~~~
When Marriage is Outlawed, Only Outlaws will have Inlaws.

Jun 27, 2007 2:43 AM in response to Gary Kerbaugh

Hi Gary. Thanks for the notes. I will review later as I'm dead tired at (close to) 6am. I want to know more about this shell stuff, how it works, what I have access to.

I downloaded a Growl tool that I want to implement from middleware. It said do this, so I had to do that. $PATH had to be modified.

My logins into this system have been giving me errors for the past 2 weeks. So much for WO.

Cheers

Adding something to my $PATH permanently

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