70

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.

2

11 Answers 11

74

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.

Change PATH:

  1. Append something to your PATH

    export PATH=$PATH:/your/new/path/here
    
  2. Override your PATH (save backup before!)

    export PATH=:/your/new/path/here:/another/new/path/here
    
2
  • 9
    It 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. May 26, 2009 at 13:18
  • 2
    Reader, if you want to edit the $PATH on Linux, not just add a path to it, see the answer to superuser.com/questions/1353497/removing-directories-from-path instead.
    – rolfedh
    May 15, 2021 at 12:44
18
PATH=$PATH:newPath1:newPAth2
export PATH
3
  • 4
    I think you can do that all on one line if you want to. export PATH=$PATH:newPath1:newPAth2
    – Chris Lutz
    May 26, 2009 at 11:03
  • 3
    It 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
    May 26, 2009 at 11:05
  • Can be used semicolon to separate commands on the same line: PATH=$PATH:newPath1:newPAth2; export PATH Apr 5 at 8:54
13

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
1
  • 2
    Editing the environment file was the only way I could get the PATH to change and stay changed.
    – Thomas Langston
    Oct 10, 2010 at 21:59
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.

2

If you're using ZSH, you can edit your paths like this:

nano ~/.zshrc
1

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

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.

1
  • (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. Aug 9, 2017 at 17:09
1

All answers intend to add to the PATH but this is how you remove an environment variable

Suppose you had the path

PATH=/home/pradan/ti-processor-sdk-linux-am57xx-evm-06.02.00.81/linux-devkit/sysroots/x86_64-arago-linux/usr/bin:...other variables...:/snap/bin

and want to remove the first variable /home/pradan/ti-processor-sdk-linux-am57xx-evm-06.02.00.81/linux-devkit/sysroots/x86_64-arago-linux/usr/bin.

  1. Open the .profile and edit it
$ gedit ~/.profile

A text editor open up with the hated variable as shown enter image description here Comment it out and after Ctrl+S, close this file.

  1. Source the script
$ source /etc/environment

And Done :)

To verify, recheck the updated path using printenv.

0

in ubuntu or linux mint you can edit the user environment by executing the command below on the terminal

nano .bashrc
0

Editing a PATH environment variable on linux especially on ubuntu can be tricky. You might want to first get familiar with .bashrc file. You may find it in your home directory (Hidden). It is a configuration file which contains the details about the terminal session. In order to edit the PATH variable, open .bashrc file in any GUI text editor or terminal using suitable text editor ( preferably nano). In that file, add following command:

export PATH=$PATH:/new_path

Note: Many websites may ask you to add the new path at the beginning, but sometimes it does not work and is never recommended to do so. Always add path to the end. Close your current terminal session and you are good to go!

0

A lot of the answers ignore what happens if you want to remove a PATH from the middle a larger PATH that's got some ad-hoc edits, so I'll address that.

It's important to remember that the PATH is just a colon-separated variable, so treating it as such with sed or awk will make life much easier. For example, to break down the PATH by numbered sources, use the following:

echo $PATH | awk -F":" {'for (i=1; i<=NF;i++) {print i, $i}'}

Using that, you can reconstruct your PATH as you see fit by defining the PATH using awk, e.g.

PATH=$(echo $PATH | awk-F: -v OFS=: {'for (i=1;i<=5;i++){print $i};{print $7,$8}'})

As others have pointed out, you can add to the PATH by adding another colon-separated value, e.g.

PATH=$PATH:/path/to/happiness

But then you can also set priorities by following that with awk as above, e.g.

PATH=$(echo $PATH:/path/to/happiness | awk-F: -v OFS=: {'for (i=1;i<=4;i++){print $i};{print $8,$5,$6,$7}'})

Where the eighth path is now prioritized right in the middle of the other seven.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .