How to add a line to .bash_profile

43

18

I just installed Homebrew on bash on my Mac and now I need to do this:

Once you’ve installed Homebrew, insert the Homebrew directory at the top of your PATH environment variable. You can do this by adding the following line at the bottom of your ~/.bashrc file.

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

"ls -a" shows .bash_profile and .bashrc.save.

Do I add this line to .bash_profile? How?

Sorry. I'm new to everything.

user275405

Posted 2013-11-19T22:53:31.377

Reputation: 531

Answers

55

You can run this command in Terminal, which will append the line to your .bash_profile:

echo 'export PATH=/usr/local/bin:$PATH' >>~/.bash_profile

arxanas

Posted 2013-11-19T22:53:31.377

Reputation: 679

21haha - just be sure to use two carats >> instead of one >. Had a friend just completely wipe mine by accident trying to append. – Ryan Tuck – 2015-04-10T16:25:04.347

How can I update existing export in a bash_profile? – Himalay – 2019-01-07T04:56:48.453

10

.bash_profile is a script that is executed each time you start a new shell. On Linux, it's called under different circumstances than .bashrc, but on OS X, they work exactly the same way. Any command you add to the file will be run whenever you open a new terminal window (thus starting a new interactive shell).

$PATH is a variable that tells the shell where to look for executable files - so when you type a command, the system will search each directory specified in that variable until it finds an executable program with that command's name.

The command export PATH=/usr/local/bin:$PATH prepends the directory /usr/local/bin to the current PATH, so it becomes the first directory searched by the shell.

.bash_profile just a normal plain text file - you can edit it with any text editor, including vi or nano, or even a graphical editor like TextEdit. It's up to you - just remember to save it as a plain-text file.

user55325

Posted 2013-11-19T22:53:31.377

Reputation: 4 693

4

open .bash_profile with your favorite tekst editor

for example you can use 'vi'

open your terminal and make sure you are in your home directory

type vi .bash_profile and press enter

in vi, type i to be able to type

enter your line

press escape

hold shift and press z twice (z z) to save and quit

that's it!

Vincent

Posted 2013-11-19T22:53:31.377

Reputation: 930

Nice you explained how to exit Vi :D – smonff – 2019-05-03T10:24:12.557

2

How to add a line to .bash_profile?

Open the TextEdit app. Its like Notepad on Windows.

Navigate to File → Open.... In the center drop down, be sure to select Home. Or, select the Home directory in the left pane. Then, use COMMAND+SHIFT+. to show hidden files:

enter image description here

Select .bash_profile and edit away...

jww

Posted 2013-11-19T22:53:31.377

Reputation: 1