81

How will I make this /media/De Soft/mongodb/bin PATH variable permanent?

Everyone is saying "export PATH=$PATH:media/De\ Soft/mongodb/bin to your ~/.profile, or .bashrc, or .zshenv depending on your shell".

I don't know what is ~/.profile, or .bashrc, or .zshenv. What do they do actually?

How will I add export PATH=$PATH:my/path to my .profile/.bashrc/.zshenv?

I'm using 64 bit Ubuntu 14.04 LTS with default terminal.

4 Answers 4

122

They are configuration files. One way:

  • Open a terminal window using Ctrl+Alt+T
  • Run the command gedit ~/.profile
  • Add the line

    export PATH=$PATH:/media/De\ Soft/mongodb/bin

    to the bottom and save

  • Log out and log in again

Edit:

A safer way is to use quotes. Doing so is necessary if one or more directories in the original PATH contain spaces. So:

export PATH="$PATH:/media/De Soft/mongodb/bin"
6
  • 1
    @GunnarHjalmarsson, do I really need to export PATH var? Maybe, it is done by default in some other script? I have checked by ~/.profile and a PATH var is there, but it is not explicitly exported: PATH=~/.local/bin:$JAVA_HOME/bin:$PATH
    – yuranos
    Commented Mar 26, 2017 at 21:12
  • 6
    @yuranos87: No, you are right; when modifying PATH in ~/.profile, exporting is redundant, since PATH already is an environment variable. Commented Mar 26, 2017 at 21:17
  • 6
    You don't need to logout and login again. Use source ~/.profile.
    – timbo
    Commented Jul 11, 2017 at 22:44
  • 1
    @timbo: That does not make the variable available to already started processes in the session (except for the current terminal). Commented Jul 11, 2017 at 23:44
  • 1
    Don't you need a quote mark in the string as in export PATH="$PATH:/media/De\ Soft/mongodb/bin"? Is it optional? Commented Dec 31, 2018 at 5:42
15

To permanently change PATH you need to make changes to /etc/environment file. Make a backup before editing:

sudo cp /etc/environment /etc/environment.bak
sudo nano /etc/environment

sample output:

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

Paths are delimited by : so to add a new path say x/y/z this will how our /etc/environment looks like:

PATH="x/y/z:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games"
2
  • Hi, i did what you wrote here and $ echo $PATH reveals: /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/snap/bin but: sudo nano /etc/environment reveals alot more. like games, i edited the path file, it shows up in NANO, but not in terminal using $echo $PATH, am i alrite? Commented Aug 30, 2020 at 1:02
  • @NaturalDemon: It doesn't matter how you did that as long as PATH variable contains it correctly you are OK.
    – Xaqron
    Commented Aug 30, 2020 at 11:05
6

Type the following in a terminal window

export PATH=/media/De\ Soft/mongodb/bin:$PATH 

Close the terminal and restart the computer. The path should include /media/De\ Soft/mongodb/bin when you type this in the terminal:

echo $PATH
4
  • 18
    That only works for current terminal session. It needs to be in ~/.profile (or ~/.bashrc on common GNU/Linux distros... even ~/.bash_profile) to be run on every console load. Commented Aug 10, 2016 at 16:38
  • agreed. but upon system restart it is loaded in the path variable. Commented Aug 10, 2016 at 19:43
  • 12
    @NiveditaVelagaleti: No it's not unless you make it persistent via a config file. The terminal command does not modify PATH persistently. Commented Mar 26, 2017 at 21:22
  • 1
    To be clear, I should not put this in ~/.bashrc or ~/.bash_profile if I want he variable (PATH) to be available to most applications, correct? The documentation says, "Shell config files such as ~/.bashrc, ~/.bash_profile, and ~/.bash_login... may work on Bash shells for programs started from the shell, [but] variables set in those files are not available by default to programs started from the graphical environment in a desktop session." Commented Jun 7, 2022 at 21:55
0

You could also just write directly to the ~/.profile from terminal:

echo 'PATH=$PATH:/media/De\ Soft/mongodb/bin' >> ~/.profile

You must log in to answer this question.

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