68

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.

102

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
    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. Mar 26 '17 at 21:17
  • 4
    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). 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? Dec 31 '18 at 5:42
12

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? Aug 30 '20 at 1: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
3
  • 17
    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. Aug 10 '16 at 16:38
  • agreed. but upon system restart it is loaded in the path variable. 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. Mar 26 '17 at 21:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.