This article should give you a good overview of the PATH Varibale and what to consider.
I only tested it with Debian and Ubuntu but it should also work with most linux operating systems.
PATH is an environment variable which specifies the directories in where your executable programs are located.
echo $PATH
/home/max/bin:/usr/local/bin:/usr/bin:/bin
shows you the directories colon separated list.
This means in my example the following directories:
/home/max/bin
/usr/local/bin
/usr/bin
/bin
The order is important in this case because the first found program is always executed.
And that's one of the reasons why I have to emphasize how important it is to specify absolute paths for automatic programs. This could pose a high security risk, for example, if someone installs a compromised version of sed (stream editor) in /usr/local/bin, although ls is usually found in /bin. The compromised version in /usr/local/bin/sed will be executed.
Keep that in mind.
The command which displays the location of the program that is used.
which cat
/bin/cat
This is very handy if you need the absolute path of a program. You can also determine which program will be executed if several binary programs with the same name exist in different directories of your PATH directories.
Alternatively, whereis -b shows more binaries even if a program exists in different directories.
whereis -b bash
bash: /bin/bash /etc/bash.bashrc
export PATH="$PATH:/dir/tmp"
export PATH="/dir/tmp:$PATH"
Just think of the colons. If you want the directory to be preferred, you should put the path at the beginning. Otherwise, to the end.
I recommend this method for testing purposes. As soon as you close your terminal, the changes are reset.
Well that's a tricky one.
There are differences between the Linux operating systems. Even between Debian and Ubuntu I would recommend different approaches (e.g. for Ubuntu I would add the directory with sudo vi /etc/environment for a system-wide configuration). Therefore you should read the documentation of your operating system in this case. However, in most cases you are well advised to adjust the configuration file of your terminal, which should be bash in most cases (Which terminal you use can be checked with echo $0).
vi ~/.bashrc
Just add the line export PATH=$PATH:/dir/bin at the end of the file and change /dir/bin to the directory you want to add and customize it as described above in temporary.
If you're new to Linux, you might want to use nano instead of vi.
That's a question I read very often, so this is an additional point.
System-wide: /usr/local/bin
User-specific: ~/bin
Please make sure that ~/bin is present in your PATH variable.
That's it,
I hope you enjoyed the text as a refresher or as information for beginners.
If you have any questions, please write them in the comments. Especially if you find any mistakes.
Cheers!
MaxPatternMan