Goal
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 TestClass.java and you get javac is not a recognized command. One way to resolve this is to do pathtojdk/bin/javac TestClass.java 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
- $HOME/mozilla/adt-bundle-linux/sdk/tools
- $HOME/mozilla/adt-bundle-linux/sdk/build-tools
- $HOME/mozilla/adt-bundle-linux/sdk/platform-tools
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.
- Edit your ~/.bashrc file and add the export line there.
- Save the file.
- Lastly execute source ~/.bashrc
and thats it. Those PATHS should be in your $PATH environment variable permanently from there on.
Caution
- 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 runls
---and then realize that the tarball had a malicious program calledls
in it. - There can be other, more nasty versions. For instance, creating a malicious script called
sl
and waiting for someone to mistypels
.