Ever want to run a program and your shell doesn’t know where to find it? How does your shell know how to find some directories and not others? The short answer is that there is a variable named: $PATH that contains a list of directories to look in. This article will focus on setting the path variable in the bash shell.
The first thing you need to know about setting the path in Linux is that the technique for setting it is shell specific. We’ll be concentrating on the Bourne-Again-Shell (bash).
Every bash user can have two files in their directory that .bash looks for at certain times. The first is .bash_profile which runs once per login. The other is the .bashrc file that runs once per interactive shell. (One that isn’t using pipes.)
Your .bash_profile file is the appropriate place to set your path. But there is a little more to it to understand. So lets take a look at a simple .bash_profile file:
# Get the aliases and functions
if [ -f ~/.bashrc ]; then
# User specific environment and startup programs
As you can see we set the PATH variable = $PATH:$HOME/bin. In other words take everything that is already in the path and tack on the “bin” subdirectory of the path defined by $HOME.
Why would we append to the path rather than just setting it? The reason is that other files that were included before this one have already set the path variable. You’ll note that this file has a strange “if [ -f ~/.bashrc] ” statement. That line (and the one after) mean if ~/.bashrc exists then run it.
So what is in my ~/.bashrc. Lets take a look:
# User specific aliases and functions
# Source global definitions
if [ -f /etc/bashrc ]; then
This file is where we would include any aliases we wanted to set up. Then we turn around and use the same trick we used before to run /etc/bashrc.
This whole files including files cycle goes on for a level or two more after this even. When it’s all said and done any script along the way could have added stuff to the path. That is why we use append to the path rather than setting it directly.
So lets show a real world example of setting the path variable. Sometimes /usr/local/bin isn’t part of your path. To set simply edit ~/.bash_profile. Somewhere in that file add the following:
After that your can either type ” . ~/bash_profile ” or just log out and then log back in. Either way your path will be set from that point on