Friday, 4 July 2014

How to permanently set $PATH on Linux?


Very basic information that every Linux user needs to know - How to export $PATH . This variable contains all the paths which may have executables that will be recognized throughout the System. For example lets say you installed java. You want to compile a program and so you do javac and you get javac is not a recognized command. One way to resolve this is to do pathtojdk/bin/javac but this is not the best way. You can add path pathtojdk/bin to that $PATH environment variable and then you can access all commands in bin folder from anywhere in your System. So lets see how can we achieve this...

Getting Started...

So lets say I want to add following 3 paths to $PATH env variable
  1. $HOME/mozilla/adt-bundle-linux/sdk/tools
  2. $HOME/mozilla/adt-bundle-linux/sdk/build-tools
  3. $HOME/mozilla/adt-bundle-linux/sdk/platform-tools
You can view your existing $PATH variable by just typing echo $PATH . To add it you can do the following. Execute the following on command line

export PATH=$PATH:$HOME/mozilla/adt-bundle-linux/sdk/tools:$HOME/mozilla/adt-bundle-linux/sdk/build-tools:$HOME/mozilla/adt-bundle-linux/sdk/platform-tools

then again you can check the $PATH variable using echo.

But wait a second. Restart the console and if you notice the exported PATH is gone. Yes it is lasts only for that console session. So lets see how can we export this $PATH permanently.

  1. Edit your ~/.bashrc file and add the export line there.
  2. Save the file.
  3. Lastly execute source ~/.bashrc
Note : source command is basically to make your console aware of the changes that you have made in the file.

and thats it. Those PATHS should be in your $PATH environment variable permanently from there on.


  • 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.  
  • 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.
  • There can be other, more nasty versions. For instance, creating a malicious script called sl and waiting for someone to mistype ls.   

Related Links

t> UA-39527780-1 back to top