1071

On Linux, how can I add a directory to the $PATH so it remains persistent across different sessions?

Background

I'm trying to add a directory to my path so it will always be in my Linux path. I've tried:

export PATH=$PATH:/path/to/dir

This works, however each time I exit the terminal and start a new terminal instance, this path is lost, and I need to run the export command again.

How can I do it so this will be set permanently?

Amin Ya
  • 1,515
  • 1
  • 19
  • 30
Ali
  • 261,656
  • 265
  • 575
  • 769
  • If you are on a mac, then `bashrc` works fine, no need to continuously `source ~/.profile` for the terminal to read from the environment variables – Jose Mhlanga Jan 16 '21 at 12:35

24 Answers24

1219

You need to add it to your ~/.profile or ~/.bashrc file. 

export PATH="$PATH:/path/to/dir"

Depending on what you're doing, you also may want to symlink to binaries:

cd /usr/bin
sudo ln -s /path/to/binary binary-name

Note that this will not automatically update your path for the remainder of the session. To do this, you should run:

source ~/.profile 
or
source ~/.bashrc
Greenbeard
  • 508
  • 5
  • 14
mpowered
  • 13,162
  • 2
  • 15
  • 18
  • 12
    A couple of questions. 1) Shouldn't there be a colon between `$PATH` and `/usr/bin`. 2) Should `/usr/bin` even be there. 3) Shouldn't you rather use `/usr/local/bin`? – Batandwa Jan 11 '14 at 00:16
  • 229
    *Please note*: it's often considered a security hole to leave a trailing colon at the end of your bash PATH because it makes it so that bash looks in the current directory if it can't find the executable it's looking for. Users who find this post looking for more information should be advised of this. – erewok Jan 14 '14 at 00:16
  • 54
    @AdamRobertson It is unsafe- consider the scenario when you unpack a tarball, then `cd` to the directory you unpacked it in, then run `ls`---and then realize that the tarball had a malicious program called `ls` in it. – Lily Chung Feb 27 '14 at 00:39
  • 22
    For me it was .bash_profile, not .profile. Seems this is different for everyone. – donquixote Apr 09 '14 at 01:08
  • 1
    @donquixote see here for the difference between those files http://stackoverflow.com/questions/415403/whats-the-difference-between-bashrc-bash-profile-and-environment – max pleaner Jun 07 '14 at 18:21
  • 3
    @Istvan Chung In that case, the system's ls would take precedence, but I see what you're saying... – mpowered Jun 10 '14 at 17:56
  • @AdamRobertson There are other, more nasty versions. For instance, creating a malicious script called `sl` and waiting for someone to mistype `ls`. – Lily Chung Jun 13 '14 at 21:27
  • 1
    FYI: In *Ubuntu* and *Debian* there is already added `~/bin` directory in `~/.profile`. – patryk.beza Jun 20 '14 at 15:07
  • 10
    I think I significantly improved the quality of this answer, and addressed a few issues which other users brought up. Every path export, or every command which adjusts the path, should always make sure to separate an existing path with a colon. Leading or trailing colons should never be used, and the current directory should never be in the path. – Erick Robertson Sep 04 '14 at 17:43
  • 4
    just to clarify. the path setup now shown here is ok and isn't dangerously including the current directory... took me a while to figure out from the other comments here, but you can see from the edit history what the bad version looks like – JonnyRaa Jan 22 '15 at 10:47
  • Shouldn't it have quotes, like `export PATH="$PATH:/path/to/dir"`, in case there are spaces in any of the directory names? I agree it's a bit unlikely in the case of `PATH`. But in any case, it's a good habit to put `"` around all expansions anyway. – Aaron McDaid Jul 29 '15 at 14:36
  • @AaronMcDaid If you have spaces in your directories, you should probably be escaping them. It's a matter of preference, really. – mpowered Aug 01 '15 at 18:19
  • 3
    Sorry to necromance this, but this did not work for me on Arch. Instead i added the line into `.bashrc`. – Alex Feb 06 '16 at 13:49
  • @Alex that would work on rhel I know for sure. Perhaps you can also alter the scripts in the /etc/bashrc file depending on access for various users. As long as a script that sets the path var each time the shell opens you get the right path var. As far security goes as long you ensure that no commands can be run in your export directory, unintentionally, exporting would secure.. – Greg Price Apr 20 '17 at 17:22
  • In my Debian 8 system, I just had to put the path in `~/.profile` file and then run `source .profile` in order to make the changes effective immediately for the current terminal session. This is set permanently and is available after logoff/reboot. – HelloWorld101 Jun 12 '17 at 12:44
  • 1
    Run `source ~/.profile` after editing `~/.profile` that you do not need to reboot. – thinwybk Jul 28 '17 at 07:58
  • How about path on Android? See my [question](https://stackoverflow.com/questions/48686351/how-to-set-path-on-android-systemwide) – sZpak Feb 09 '18 at 23:04
  • This is a temporary solution because all changes would be lost on system boot. Correct solution is to edit the PATH variable in the /etc/environment file. – aog Mar 29 '18 at 08:18
  • @aog that’s incorrect, this is executed at the start of shell session and thus persists across logins. – mpowered Mar 30 '18 at 11:56
  • In newer Ubuntu releases (16.04/18.04) I'm using .profile to extend the $PATH variable, because the entries will be duplicated in new bash shells when added in .bashrc. – Peter Gloor Jun 13 '18 at 18:16
  • 1
    Expanding @erewok's most popular comment (like I needed to). The trailing colon (i.e. colon as last character) is treated as a ZERO-length folder name, similar to that of a pair of two consecutive colons. And the convention is to treat all ZERO-length folder name as equivalent to *current directory*. – Loves Probability Jun 19 '18 at 06:31
  • on cent os7, i seem to have `.bash_profile` instead of `.profile` – StackAttack Jun 20 '18 at 17:26
  • Just for everyone's information, I realized just now this can be used as an exploit for https://exploit-exercises.com/nebula/level01/ – yobro97 Jul 17 '18 at 12:10
  • this answer is correct only for certain methods. eg .profile does not get executed on gnome with wayland. To put it bluntly the whole thing is a mess. – RichieHH Mar 05 '20 at 20:07
  • 1
    @RichieHH yes, your mileage may vary depending on your distribution. You’ll at least need to know which file your shell is using to have this work properly, and that’s pretty simple. – mpowered Mar 06 '20 at 22:53
  • I'm no stranger to Linux but it took me half a day get it right on setting my system path on Wayland with Gnome on Debian 10. In case anyone finds this the only way that worked "for me" was to use /etc/security/pam_env.conf. Hordes of other documented solutions such as /etc/environment, ~/.pam_environment, .config/environment.d/ xx-f.conf didn't work. The line I added was: PATH DEFAULT=${HOME}/bin:${HOME}/.local/bin:/usr/local/bin:/usr/bin:/bin:/usr/games – RichieHH Mar 07 '20 at 11:44
  • What if you use `zsh` or some other shell? – Leo Jun 30 '23 at 16:59
  • @Leo then you would add it to the file relevant for your shell. I would do .zshrc on my system, but there are multiple options that will all work. – mpowered Jul 05 '23 at 20:38
  • @mpowered Is there some good way of finding the rc file of the user's shell? – Leo Jul 06 '23 at 11:10
  • @mpowered Something that would work in a shell script – Leo Jul 06 '23 at 11:20
386

There are multiple ways to do it. The actual solution depends on the purpose.

The variable values are usually stored in either a list of assignments or a shell script that is run at the start of the system or user session. In case of the shell script you must use a specific shell syntax and export or set commands.

System wide

  1. /etc/environment List of unique assignments. Allows references. Perfect for adding system-wide directories like /usr/local/something/bin to PATH variable or defining JAVA_HOME. Used by PAM and systemd.

  2. /etc/environment.d/*.conf List of unique assignments. Allows references. Perfect for adding system-wide directories like /usr/local/something/bin to PATH variable or defining JAVA_HOME. The configuration can be split into multiple files, usually one per each tool (Java, Go, and Node.js). Used by systemd that by design do not pass those values to user login shells.

  3. /etc/xprofile Shell script executed while starting X Window System session. This is run for every user that logs into X Window System. It is a good choice for PATH entries that are valid for every user like /usr/local/something/bin. The file is included by other script so use POSIX shell syntax not the syntax of your user shell.

  4. /etc/profile and /etc/profile.d/* Shell script. This is a good choice for shell-only systems. Those files are read only by shells in login mode.

  5. /etc/<shell>.<shell>rc. Shell script. This is a poor choice because it is single shell specific. Used in non-login mode.

User session

  1. ~/.pam_environment. List of unique assignments, no references allowed. Loaded by PAM at the start of every user session irrelevant if it is an X Window System session or shell. You cannot reference other variables including HOME or PATH so it has limited use. Used by PAM.

  2. ~/.xprofile Shell script. This is executed when the user logs into X Window System system. The variables defined here are visible to every X application. Perfect choice for extending PATH with values such as ~/bin or ~/go/bin or defining user specific GOPATH or NPM_HOME. The file is included by other script so use POSIX shell syntax not the syntax of your user shell. Your graphical text editor or IDE started by shortcut will see those values.

  3. ~/.profile, ~/.<shell>_profile, ~/.<shell>_login Shell script. It will be visible only for programs started from terminal or terminal emulator. It is a good choice for shell-only systems. Used by shells in login mode.

  4. ~/.<shell>rc. Shell script. This is a poor choice because it is single shell specific. Used by shells in non-login mode.

Notes

GNOME on Wayland starts a user login shell to get the environment. It effectively uses the login shell configurations ~/.profile, ~/.<shell>_profile, ~/.<shell>_login files.

Man pages

Distribution-specific documentation

Difference between Login Shell and Non-Login Shell?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Grzegorz Żur
  • 47,257
  • 14
  • 109
  • 105
  • 8
    Thank you for the detailed answer, this should be higher up. Maybe `.bash_profile` should be added to the list as well? – James Ko Nov 11 '16 at 23:12
  • 4
    @JamesKo that was number 4 – trve.fahad Nov 14 '16 at 04:11
  • 1
    I think the best answer is suggested `/etc/environment`. But can I refresh it without logging out and in? Sometimes I don't use bash or sh so `source /etc/environment` doesn't work. – banan3'14 Apr 08 '18 at 20:22
  • 2
    Great and in my opinion the most complete answer. Should be much higher up. – Peter Gloor Jun 13 '18 at 18:23
  • 2
    My reason to search for this topic was actually Go. Glad to see I'm not the only one who realised that .bashrc is not the right place. ;) – Peter Gloor Jun 13 '18 at 18:34
  • 1
    I've found that `/etc/environment.d` won't set variables for user login shells. According to the creator of systemd, ["Login shells (such as those started via getty on the console or ssh) are not children of systemd and hence the generators \[in `/etc/environment.d`\] have no effect on them whatsoever."](https://github.com/systemd/systemd/issues/12938#issuecomment-508381592) – Chai T. Rex Jan 31 '20 at 02:09
  • Gnome on Wayland does not source .profile. - certainly not on Debian/Ubuntu – RichieHH Mar 05 '20 at 20:05
  • @RichieHH It's more complicated than that; Bash, Gnome, and Ubuntu all have default behaviors which may or may not be changed by the user's customizations. Perhaps it would be correct to say that Gnome on Ubuntu works a particular way out of the box if the user's login shell is Bash with the default login files. But most users customize their shell's startup files and their Gnome session. A common arrangement is to have `.bash_profile` pull in `.profile` if it exists, but I don't know if Gnome runs a Bash login shell as part of its session startup these days. – tripleee May 25 '21 at 14:43
  • What does "allows references" mean? Please note [variable expansion does not work in /etc/environment](https://archive.is/tdKJR#selection-1295.0-1299.16) – MrR May 27 '21 at 20:19
  • @tripleee thats what I said. It's not straightforward AND it depends on distro as well as WM. – RichieHH Jun 22 '21 at 09:02
  • ubuntu 21.04 + wayland + zsh did not pickup `~/.profile` however `~/.zprofile` worked. – NiMa Thr Aug 14 '21 at 20:04
262

In Ubuntu, edit /etc/environment. Its sole purpose is to store environment variables. Originally the $PATH variable is defined here.

This is a paste from my /etc/environment file:

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

So you can just open up this file as root and add whatever you want.

For immediate results,

Run (try as normal user and root):

source /etc/environment && export PATH

If you use Z shell (zsh), add this line right after the comments in /etc/zsh/zshenv file:

source /etc/environment

I encountered this little quirk on Ubuntu 15.10 (Wily Werewolf), but if your zsh is not getting the correct PATH, this could be why.

trve.fahad
  • 4,397
  • 2
  • 17
  • 25
  • 18
    Not all system have /etc/environment – user3439968 Nov 14 '14 at 17:20
  • @user3439968 While you might be correct, I never encountered one without it :D Then again I only mess around with OS that I am comfortable using. – trve.fahad Nov 15 '14 at 00:33
  • 1
    My Ubuntu and Debian has it. My FreeBSD and OpenWRT don't. I use /etc/profile – user3439968 Nov 15 '14 at 14:24
  • 12
    FWIW `$PATH` is also defined in `/etc/profile` in Arch Linux. – Sparhawk Feb 24 '15 at 01:57
  • Funny enough, my Ubuntu 14.04 doesn’t have that file either as it uses `~/.bashrc`. On the other hand, not having a `/etc/environment` makes sense from my point of view me as I don’t have a single variable I need to set for *“everyone”*… and it’s generally best to separate the core system configurations from individual user configurations anyway. ;) – e-sushi Feb 25 '15 at 16:48
  • 3
    @e-sushi I am actually shocked at that. I'm on ubuntu 14.04.1 myself. and I can promise you the file came built in. – trve.fahad Feb 28 '15 at 12:18
  • 3
    After trying every suggestion under the sun but /etc/environment, and having them all NOT work, i finally stumbled across this. I am also on Ubuntu 14.04 and this is the only one that actually changed the PATH variable after reboots. – Chockomonkey Mar 24 '15 at 16:21
  • it took a restart, for the system to realize I added that line! was not immediately recognized – Developerium Sep 27 '15 at 05:16
  • 2
    `/etc/environment` is for system-wide environment variables. This is the only reason to change it. If you make a mistake while you are editing this file, you'll probably end up with an empty PATH variable (I wouldn't wish on any man). A good way to add paths to your PATH variable that affect the system as a whole is to use `/etc/profile.d` directory (this link should be an useful [link](https://help.ubuntu.com/community/EnvironmentVariables)). –  Jan 12 '16 at 21:14
  • @BlackBrain, yes I am aware of the dangers that come with modifying `/etc/environment`. but if you use anything like gedit, you should, by default, have a backup file. not to mention, you could always end up overriding PATH to a blank variable in **ANY** system-wide file. my point is, if you are willing to modify PATH, you should be already acquainted with the dangers of doing it and modifying `/etc/environment` is not any more dangerous than say something like `/etc/profile` :) – trve.fahad Jan 18 '16 at 18:00
  • 4
    User should restart the PC after updating environment file. – Harish_N Mar 24 '16 at 17:50
  • For many of us who use ~/.bashrc you can easily load (e.g. if you have changed your $PATH variable but don't want to restart) the same way using source ~/.bashrc – Brian Jackson Oct 04 '17 at 18:58
  • 1
    Debian has `/etc/environment` but setting PATH there has no effect - /etc/profile and /etc/login.defs re-set them backs. Tested on Debian 9.5 – Tankman六四 Oct 08 '18 at 02:46
86

For Bash, you can put the export declaration in ~/.bashrc. For example, my .bashrc contains this line:

export PATH=/var/lib/gems/1.8/bin:/home/ash/.bin:$PATH
ashastral
  • 2,818
  • 1
  • 21
  • 32
  • 10
    restart needed? – Ali Feb 01 '13 at 01:01
  • 2
    Worked when I put this in the `.profile`', didn't find `.bashrc` – Ali Feb 01 '13 at 01:10
  • It might be dependent on the exact system; I'm not sure exactly what conditions determine which file is executed. Glad the problem was solved, though. – ashastral Feb 01 '13 at 02:10
  • 14
    @Click Upvote You need to do ```source ~/.bashrc``` to to reload ```.bashrc``` configuration. Then it will work – BigSack Apr 06 '14 at 06:02
  • How I'll `Put the export declaration in ~/.bashrc.` I'm using Ubuntu 14.04 LTS. I'm very newbie to Linux. – Towhid Jul 21 '14 at 18:40
  • 6
    The `export` keyword is only needed if `PATH` is not already flagged as an environment variable -- which it almost unconditionally will be. Simply `PATH=/var/lib/gems/1.8/bin:/home/fraxtil/.bin:$PATH` would have the same effect. – Charles Duffy Oct 04 '14 at 17:20
  • @Towhid open up the file (`~/.bashrc`) in a text editor like *nano* and type up that line at the endo of the file. The path will be modified as soon as you open a new *bash* instance – trve.fahad Nov 15 '14 at 00:37
  • This worked for me on a CentOs system. I'm sure its different for different distros. – G_Style Aug 21 '18 at 14:21
  • Can you add information on which system(s) (distribution and version) this was tested? – Peter Mortensen Jul 25 '21 at 10:31
  • No, because this answer was written in 2013 and I have no idea what I was using back then. The accepted answer is better anyway. – ashastral Jul 28 '21 at 01:30
35

You may set $PATH permanently in two ways.

  1. To set the path for a particular user:

    You may need to make the entry in file .bash_profile in the home directory for the user.

    E.g, in my case I will set the java path in the Tomcat user profile*

     echo "export PATH=$PATH:/path/to/dir" >> /home/tomcat/.bash_profile
    
  2. To set a common path for all system users, you may need to set the path like this:

     echo "export PATH=$PATH:/path/to/dir" >> /etc/profile
    
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Mohit M
  • 769
  • 7
  • 15
  • 3
    Is the file named `/etc/profiles` with an `s` on your distro? Mine has no `s`. I think you have a typo. – Chris Johnson Oct 16 '14 at 13:36
  • 4
    You probably want to escape the $ you are writing to the profile file. e.g. echo "export PATH=\$PATH:/path/to/dir" >> /etc/profile, that way you actually append to the variable when that script runs rather than setting it to a literal value based on it's value at the time of executing this initial command. – BuvinJ Jan 20 '16 at 15:12
  • `echo "export PATH=$PATH:/path/to/dir" >> /home//.bashrc` this worked for WSL with Kali distrubution. A new thing got that you can set any variable like `echo "export PYTHONPATH=/path/to/dir" >> /home//.bashrc` for new creation and `echo "export PYTHONPATH=$PYTHONPATH:/path/to/dir" >> /home//.bashrc` this for update. – Akash Shendage Jun 08 '22 at 11:20
17

You can use on CentOS or Red Hat Linux (RHEL) for the local user:

echo $"export PATH=\$PATH:$(pwd)" >> ~/.bash_profile

This adds the current directory (or you can use another directory) to the PATH. This makes it permanent, but it takes effect at the next user logon.

If you don't want do a re-logon, then you can use:

source ~/.bash_profile

That reloads the # User specific environment and startup programs. This comment is present in file .bash_profile.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
13

You can also set it permanently, editing one of these files:

/etc/profile (for all users)

~/.bash_profile (for current user)

~/.bash_login (for current user)

~/.profile (for current user)

You can also use /etc/environment to set a permanent PATH environment variable, but it does not support variable expansion.

Extracted from: Linux: Añadir ruta al PATH

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Delucaramos
  • 131
  • 1
  • 2
11

I think the most elegant way is:

  1. Add this in the ~/.bashrc file.

    Run this command:

     gedit ~/.bashrc
    

    Add your path inside it:

     export PATH=$PATH:/opt/node/bin
    
  2. source ~/.bashrc

(Ubuntu)

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Himanshu sharma
  • 7,487
  • 4
  • 42
  • 75
  • 2
    Didn't you leave out important bits when you plagiarised [Gimcuan Hui's answer](https://stackoverflow.com/questions/14637979/how-to-permanently-set-path-on-linux-unix/38417461#38417461)? – Peter Mortensen Jul 25 '21 at 12:04
11
  1. Modify the "/etc/profile" file:

    vi /etc/profile
    

    Press the I key to enter editing mode and move the cursor to the end of the file. Additional entries:

    export PATH=$PATH:/path/to/dir;
    

    Press the Esc key to exit edit mode, and :wq to save the file.

  2. Make the configuration effective

    source /etc/profile
    

    Explanation:

    The profile file works for all users. If you want it to be valid only for the active user, change the ".bashrc" file.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Jia
  • 334
  • 3
  • 4
9

I stumbled across this question yesterday when searching for a way to add a folder containing my own scripts to the PATH - and was surprised to find out that my own ~/.profile file (on Linux Mint 18.1) already contained this:

# set PATH so it includes user's private bin if it exists
if [ -d "$HOME/bin" ] ; then
    PATH="$HOME/bin:$PATH"
fi

Thus, all I had to do was create the folder ~/bin and put my scripts there.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
RobertG
  • 1,550
  • 1
  • 23
  • 42
  • 1
    Also on [Ubuntu MATE 20.04](https://en.wikipedia.org/wiki/Ubuntu_MATE#Releases) (Focal Fossa). Standard for all [Debian](https://en.wikipedia.org/wiki/Debian)-based distributions? Or even wider than that? – Peter Mortensen Jul 25 '21 at 11:47
  • oops, overlooked the question :( Well, the files + that bash considers their content, that's pretty much a bash built-in. So, if the snippet above is not there for you, yet, you can just easily add it. This looks good as a reference https://www.baeldung.com/linux/bashrc-vs-bash-profile-vs-profile And yes, it's there on Focal for me (gnome though):) – RobertG Oct 17 '21 at 15:03
7

You can add that line to your console configuration files (e.g., .bashrc, or to .profile).

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
aqua
  • 3,269
  • 28
  • 41
6

After so much research, I found a simple solution for this (I am using Elementary OS), inspired by Flutter – Step by Step Installation on Linux – Ubuntu.

Run the following command to open the .bashrc file in edit mode. (You may also use vi or any other editor).

~$ sudo nano ~/.bashrc

Add the following line at the end of the file and save.

export PATH="[FLUTTER_SDK_PATH]/flutter/bin:$PATH"

For example:

export PATH="/home/rageshl/dev/flutter/bin:$PATH"

enter image description here

I believe this is the permanent solution for setting the path in Flutter in a Ubuntu distribution.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Ragesh P Raju
  • 3,879
  • 14
  • 101
  • 136
5

One way to add a permanent path, which worked for me, is:

cd /etc/profile.d
touch custom.sh
vi custom.sh 
export PATH=$PATH:/path according to your setting/

Restart your computer and here we go; the path will be there permanently.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
user6393373
  • 51
  • 1
  • 1
  • 2
    You don't actually need to restart your computer. Logging out and back in again is sufficient. This is quite an elegant solution as it is very easy to backout any changes without needing to edit files. It is also easy to specify an if statement so that if a directory doesn't exist on the system, it isn't added to the PATH variable. – Warwick Dec 18 '19 at 21:42
  • You need to use `sudo` to edit system files. You mustn't use Bash-only syntax in these files, which are shared with non-Bash shells. You can probably just remove the `export` as presumably your existing configuration already takes care of `export`ing this variable. If not, add `export PATH` on a separate line instead. – tripleee Aug 01 '20 at 13:18
  • Also, `/etc/profile.d` is platform-specific; if this directory doesn't exist, check `/etc/profile` and see if it has options to source (aka `.`) files which are not managed by your OS so you don't have to edit this system file directly. – tripleee Aug 01 '20 at 13:21
5

It can be directly added by using the following command:

echo 'export PATH=$PATH:/new/directory' >> ~/.zshrc
source ~/.zshrc
abdusco
  • 9,700
  • 2
  • 27
  • 44
4

Permanently add to the PATH variable

Global:

echo "export PATH=$PATH:/new/path/variable" >> /etc/profile

Local (for the current user only):

echo "export PATH=$PATH:/new/path/variable" >> ~/.profile

For global, restart. For local, relogin.

Example

Before:

$ cat /etc/profile

#!/bin/sh

export PATH=/usr/bin:/usr/sbin:/bin:/sbin

After:

$ cat /etc/profile

#!/bin/sh

export PATH=/usr/bin:/usr/sbin:/bin:/sbin
export PATH=/usr/bin:/usr/sbin:/bin:/sbin:/new/path/variable

Alternatively you can just edit file "profile":

$ cat /etc/profile

#!/bin/sh

export PATH=/usr/bin:/usr/sbin:/bin:/sbin:/new/path/variable

Another way (thanks gniourf_gniourf):

echo 'PATH=$PATH:/new/path/variable' >> /etc/profile

You shouldn't use double quotes here! echo 'export PATH=$PATH:/new/path/variable'... And by the way, the export keyword is very likely useless as the PATH variable is very likely already marked as exported. – gniourf_gniourf

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
user3439968
  • 3,418
  • 1
  • 18
  • 15
  • 1
    Nope. You shouldn't use double quotes here! `echo 'export PATH=$PATH:/new/path/variable'`... And by the way, the `export` keyword is very likely useless as the `PATH` variable is very likely already marked as exported. – gniourf_gniourf Nov 14 '14 at 17:48
  • Nope, you should use double quotes because $PATH in single quotes not interpolated. And BTW export also useful. – user3439968 Nov 14 '14 at 17:58
  • I got it. You can use double quotes or use single quotes, because $PATH interpolated when the echo executed or interpolate when /etc/profile execute. – user3439968 Nov 14 '14 at 18:14
  • 1
    @user3439968 actually, Double quotes will cause a lot of issues if you were to append to $PATH from multiple files. Consider: when you use double quotes, $PATH gets translated to a static string with all the previously defined PATH directories. say you append `/usr/local` to it using `~/.bashrc`. now if you intend to append `/opt/bin` to the same variable using `/etc/bash.bashrc`; $PATH will translate to the same static string, as a result $PATH will be **replaced** instead of appended to... It will be a matter of system's preference to one file over another – trve.fahad Jan 27 '15 at 16:20
4

Add script file [name_of_script].sh to the /etc/profile.d folder with the line:

export PATH=$PATH:/dir

Every script within the /etc/profile.d folder is automatically executed by /etc/profile on login.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Yuriy Vasylenko
  • 3,031
  • 25
  • 25
  • It's recommended way of how to customize your environment – Yuriy Vasylenko Apr 10 '15 at 12:14
  • 2
    This is only if you want the settings to be system-wide, which is probably not the most common use case. Most people want (or should want) the path to be set locally, because most users/roles are doing contextually different operations, and the fewer assumptions you make, the better. – mpowered Apr 10 '15 at 20:25
  • @mpowered, yeah, this is only for system-wide. If you want localy change PATH you should add the same export in ~/.profile or ~/.bashrc. Here you should consider that login shells read ~/.profile and interactive shells read ~/.bashrc. This is very important because ssh for example does not do an login, therefore ~/.profile will not be read. Several distibution like suse source ~/.bashrc in /etc/profile. But it's not common for all linux' – Yuriy Vasylenko Apr 15 '15 at 09:49
  • If you are referencing executable scripts/apps, don't forget to `source [name_of_script].sh` to make these apps instantly available in the current shell – ObiHill Nov 18 '20 at 14:12
4

My answer is in reference to the setting up of a Go environment on Ubuntu Linux (amd64). I have faced the same trouble of setting the path of environment variables (GOPATH and GOBIN), losing it on terminal exit and rebuilding it using the source <file_name> every time.

The mistake was to put the path (GOPATH and GOBIN) in ~/.bash_profile file. After wasting a few good hours, I found that the solution was to put GOPATH and GOBIN in the ~/.bash_rc file in the manner:

export GOPATH=$HOME/go
export GOBIN=$GOPATH/bin
export PATH=$PATH:$GOPATH:$GOBIN

And in doing so, the Go installation worked fine and there were no path losses.

The reason with which this issue can be related is that settings for non-login shells, like your Ubuntu terminal or GNOME terminal where we run the Go code, are taken from the ~./bash_rc file and the settings for login shells are taken from ~/.bash_profile file. And from the ~/.profile file if the ~/.bash_profile file is unreachable.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Abhiroj Panwar
  • 485
  • 9
  • 19
3

The files where you add the export command depends on if you are in login-mode or non-login-mode.

If you are in login-mode, the files you are looking for are either /etc/bash or /etc/bash.bashrc.

If you are in non-login-mode, you are looking for the file /.profile or for the files within the directory /.profiles.d

The files mentioned above is where the system variables are.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Dikinha
  • 31
  • 1
3

Zues77 has the right idea. The OP didn't say "How can I hack my way through this?". The OP wanted to know how to permanently append to $PATH:

sudo nano /etc/profile

This is where it is set for everything and is the best place to change it for all things needing $PATH.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Joe D
  • 39
  • 1
3

Let's say you're running macOS. You have a binary you trust and would like to make available across your system, but don't necessarily want the directory in which the binary is to be added to your PATH.

You can opt to copy/move the binary to /usr/local/bin, which should already be in your PATH. This will make the binary executable like any other binary you may already have access to in your terminal.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Leo
  • 449
  • 6
  • 5
3

This is a one-liner. It adds a line to the .bashrc. That line is going to check if the directory has already been added to the path and append if not. This will prevent duplicating your directory in the path every time you source .bashrc.

echo "[[ \":\$PATH:\" != *\":$(pwd)/path/to/add:\"* ]] && export PATH=\"\${PATH:+\${PATH}}:$(pwd)/path/to/add\"" >> ~/.bashrc

source ~/.bashrc
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
sr9yar
  • 4,850
  • 5
  • 53
  • 59
  • 2
    If you single-quote the whole thing, you don't have to backslash-escape all the embedded double quotes. – tripleee Aug 01 '20 at 13:23
2

The simplest way is the following line,

PATH="<directory you want to include>:$PATH"

in your .bashrc file in the home directory.

It will not get reset even if you close the terminal or reboot your PC. It's permanent.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Alex Jones
  • 768
  • 9
  • 23
2

I think the most elegant way is:

  1. Add this in the ~./bashrc file:

    if [ -d "new-path" ]; then
      PATH=$PATH:new-path
    fi
    
  2. source *~/.bashrc*

(Ubuntu)

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
0

For a Debian distribution, you have to:

  • edit file ~/.bashrc. E.g: vim ~/.bashrc
  • add export PATH=$PATH:/path/to/dir
  • then restart your computer. Be aware that if you edit file ~/.bashrc as root, your environment variable you added will work only for root
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
onlyme
  • 3,776
  • 2
  • 23
  • 17
  • If you are on a mac, then `bashrc` works fine, no need to continuously `source ~/.profile` for the terminal to read from the environment variables – Jose Mhlanga Jan 16 '21 at 12:34
  • Yes, restarting does seem to be required. But what is the reason? Isn't there another way? – Peter Mortensen Jul 25 '21 at 12:31
  • 1
    The simple fix is to log out and back in. A common alternative solution is `source .bashrc` but it will re-execute everything in there, so you might end up with some things doubled or etc. Some users take care to make their `.bashrc` idempotent (check if something was already done and don't do it again if that could have undesired consequences) but that makes it more complicated. Of course, if you know what you are doing, just manually make whatever changes are necessary in the currently running shell. – tripleee Feb 27 '23 at 15:48