$PATH is very important environment variable and knowing how to work with it, can be very useful
What Is $PATH on Linux, and How Does It Work?
Every time you run a command, it is the $PATH environment variable which is being used to determine where to look for command. One common location is the /bin directory. It’s a directory that contains some of the most commonly used commands such as pwd, ls, cp, mkdir, touch, dd, df, du, tail, less, more, and etc.
You can display your $PATH using the echo command. Here’s the command.
1 | echo $PATH |
$PATH variable is very useful because it allows the command to be executed without knowing or specifying the full path to the command.
You should note that every single entry into PATH is separated by (:) semicolon sign.
Adding a Directory to Your $PATH
Example I want to add my SCRIPTS directory into $PATH
- Do cd ~/SCRIPTS and run pwd
- You should be see the full path of your Directory /home/ME/SCRIPTS
There are two ways do it — little work and another quick command 🙂
Option#1
- Once you got the output from pwd and know the full path
- open your editor and edit ~/.bashrc file
- nano ~/.bashrc
- Scroll down in the file
- Type at the end of file this line
1 | export PATH="/home/ME/SCRIPTS:$PATH |
- CTRL+X – YES to save the file
- you can also run source ~/.bashrc
- Verify now the $PATH by running echo
1 | echo $PATH |
And now you should see the Directory included in your $PATH
Option #2
This second option is much simpler and fast
- Go inside of the directory /home/ME/SCRIPTS
- Run this command
echo “export PATH=$(pwd):\${PATH}” >> ~/.bashrc
1 2 | source ~/.bashrc echo $PATH |
- This command will add to your path the Directory in which you at right now,
- refresh and reload the ~/.bashrc
- output the $PATH so you can verify that the directory has been added into your $PATH.
Enjoy!