Adding something to my $PATH permanently
apple Verified ·
Imported from the Apple Support Forums via the Wayback Machine 🔗
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
9 replies
Sign in or join Beachball Help to reply.
- 0
Mark Jalbert ·
Imported from the Apple Support Forums via the Wayback Machine 🔗
This assumes you have neither a user .profile or .bash_profile file. In each case the commands will overwrite an existing file. Choose one of the two files to set your PATH.
echo "export PATH="/usr/local/bin:$PATH"" > .profile
echo "export PATH="/usr/local/bin:$PATH"" > .bash_profile
- 0
Asher_Langton ·
Imported from the Apple Support Forums via the Wayback Machine 🔗
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 $PATHSo 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)
- 0
BioRich ·
Imported from the Apple Support Forums via the Wayback Machine 🔗
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.
- 0
Asher_Langton ·
Imported from the Apple Support Forums via the Wayback Machine 🔗
&> 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)
- 0
BioRich ·
Imported from the Apple Support Forums via the Wayback Machine 🔗
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
- 0
Asher_Langton ·
Imported from the Apple Support Forums via the Wayback Machine 🔗
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)
- 0
BioRich ·
Imported from the Apple Support Forums via the Wayback Machine 🔗
Excellent. Worked. I opened in BBEdit, cause it doesn't suck.
The explanation helped. I spend time in the terminal (and Pico), but don't know environment variables.
Cheers
- 0
Gary Kerbaugh ·
Imported from the Apple Support Forums via the Wayback Machine 🔗
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. - 0
BioRich ·
Imported from the Apple Support Forums via the Wayback Machine 🔗
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