How can I edit the $PATH on linux?

46

14

I am using ubuntu 9.04 I need to add some folder to my $PATH. I know how to read the path:

echo $PATH

I want to be able to edit it and add 2 other paths.

Thanks

Arthur

Posted 2009-05-26T10:53:39.623

Reputation: 511

2

Once you're able to d this you may want to perform more sophisticated operations on PATH: http://stackoverflow.com/questions/273909/how-do-i-manipulate-path-elements-in-shell-scripts

– dmckee --- ex-moderator kitten – 2009-05-26T13:44:38.030

Belongs on Unix or Ubuntu sites. – Thomas Bratt – 2012-10-15T15:16:57.810

Answers

45

To permanently store your path, you have a few options.

I suggest you read the Ubuntu community wiki on Environment Variables but the short answer is the best place is ~/.profile for your per-user PATH setting or /etc/profile for global settings.

Do something like export PATH=$PATH:/your/new/path/here

akent

Posted 2009-05-26T10:53:39.623

Reputation: 640

8It is important to note that there are many occasions your profile is not run (such as when a script is run by cron). If you need a specific path to be set in PATH, a script must set that path. That said, scripts should never rely on anything being in their paths and should always use absolute paths, anything else is a security issue. – Chas. Owens – 2009-05-26T13:18:04.183

14

PATH=$PATH:newPath1:newPAth2
export PATH

Glen

Posted 2009-05-26T10:53:39.623

Reputation: 241

3I think you can do that all on one line if you want to. export PATH=$PATH:newPath1:newPAth2 – None – 2009-05-26T11:03:02.223

2It depends on the shell you're using. On Solaris (I know the question is about Linux) one of the shells (can't remember which one off the top of my head) requires that you do the export separately from setting the value in a script. So I've just gotten into the habit of doing it on 2 lines. – Glen – 2009-05-26T11:05:59.130

7

You can also put this in the global environment:

sudo emacs /etc/environment

Append to the entries already in your path

PATH="/path/to/file:/other/paths"

Reload the environment

source /etc/environment

jaketrent

Posted 2009-05-26T10:53:39.623

Reputation: 171

1Editing the environment file was the only way I could get the PATH to change and stay changed. – None – 2010-10-10T21:59:15.483

2

It has already been answered on how to do that, but I'd like to give you a little tip. Here is whatI do:

I have a directory called .bash.d in my $HOME and within that I keep a set of shell scripts that do stuff to my environment (for instance setup maven correctly, modify the path, set my prompt etc.). I keep this under version control by using git, which makes it easy to go back to a working version of your env, if you screw something up badly. To get all the modifications, I simply source all files in that dir at the end of my .bashrc like this:

for i in $HOME/.bash.d/*; do source $i; done
unset i

This gives you a very flexible environment that you can easily modify and restore + you are able to export it to other machines just by using git.

André

Posted 2009-05-26T10:53:39.623

Reputation: 121

1

A variant from above, if you don't want to change the /etc/profile file directly. You can create a new file yourpath.sh in the /etc/profile.d/ directory. Then edit this file like that. With vim editor (but feel free to edit it with another editor): vim /etc/profile.d/yourpath.sh

MYPATH='/your/new/path/'
export MYPATH
export PATH=$PATH:$MYPATH

:write and quit and it's done your path has been modified. If your are using the terminal, close it and reopen it . your new variable will be updated. Now it is cleaner, you can remove this file when you don't need it anymore and it doesn't interfer with the initial configuration.

Arthur Vandergood

Posted 2009-05-26T10:53:39.623

Reputation: 11

(1) As long as PATH is exported, MYPATH doesn't need to be (unless you need it for other purposes).  (2) The quick way to save (write) and quit in vim is ZZ — no : or (Enter) required. – Scott – 2017-08-09T17:09:40.213

0

echo PATH=$PATH:path1:path2 > tmp

Edit the file tmp with your favourite text editor so the value of PATH is exactly what you want

. ./tmp

mealnor

Posted 2009-05-26T10:53:39.623

Reputation: 1