80

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.

Kevin Bowen
  • 19,395
  • 55
  • 76
  • 81
Towhid
  • 3,955
  • 3
  • 16
  • 11

4 Answers4

119

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"
Gunnar Hjalmarsson
  • 32,938
  • 3
  • 63
  • 94
  • 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 Mar 26 '17 at 21:12
  • 6
    @yuranos87: No, you are right; when modifying `PATH` in `~/.profile`, exporting is redundant, since `PATH` already is an environment variable. – Gunnar Hjalmarsson Mar 26 '17 at 21:17
  • 5
    You don't need to logout and login again. Use `source ~/.profile`. – timbo Jul 11 '17 at 22:44
  • 1
    @timbo: That does not make the variable available to already started processes in the session (except for the current terminal). – Gunnar Hjalmarsson Jul 11 '17 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? – Bruno Bentzen Dec 31 '18 at 05:42
  • @BrunoBentzen: Good question. I edited the answer to clarify. – Gunnar Hjalmarsson Dec 31 '18 at 07:04
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"
Xaqron
  • 1,262
  • 11
  • 14
  • 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? – NaturalDemon Aug 30 '20 at 01:02
  • @NaturalDemon: It doesn't matter how you did that as long as `PATH` variable contains it correctly you are OK. – Xaqron Aug 30 '20 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
  • 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. – Alejandro Iván Aug 10 '16 at 16:38
  • agreed. but upon system restart it is loaded in the path variable. – Nivedita Velagaleti Aug 10 '16 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. – Gunnar Hjalmarsson Mar 26 '17 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](https://help.ubuntu.com/community/EnvironmentVariables#Persistent_environment_variables) 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." – Randy Cragun Jun 07 '22 at 21:55
0

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

echo 'PATH=$PATH:/media/De\ Soft/mongodb/bin' >> ~/.profile
Niko Föhr
  • 1,441
  • 2
  • 16
  • 25