So, how do you set the PATH variable in Bash?
That’s a very simple task, which should take less than a minute to complete. However, for the benefit of those not familiar with the Bash shell or the PATH variable, let’s start with a very brief introduction.
Note: The operating assumption in this article is that you have access to a machine running an operating system that uses Bash and that you know how to launch a shell terminal. Specifically for this tutorial, the assumption is that you’re running a Linux distribution. If you are reading this, you probably are. And if you’re and (you) don’t know what a shell terminal is or how to launch it, take a few minutes to read How to launch and use a shell terminal in Linux.
Now, about that brief introduction to the PATH variable and Bash.
The Bash shell is a command language interpreter for Linux and other Unix-like operating systems. There are other shells like it, but it has become the most popular, and it is the default shell in Linux and Mac OS X. Bash was designed as a free replacement for the Bourne shell, one of the earliest shells for the Unix operating system. It (the Bourne shell) was named after its creator – Stephen Bourne.
When Brian Fox wrote a shell as a replacement for the Bourne shell, he called his BASH, which is an acronym for Bourne Again SHell, itself a play on the Bourne shell or the last name of its creator. When spoken, it sounds like born again, like there’s a religious angle to it. But trust me, it’s got nothing to do with religion!
The PATH variable (always written in all caps) is an environment variable that contains a colon-delimited list of system directories. The directories are where commands are located, so that when you type a command, those directories are where the system searches for it. If the command is not in any of the directories in PATH, the system outputs a “command not found message.”
To see the value of the PATH variable on your system, type: echo $PATH. The output should be similar to the one below. (The $ sign before a variable’s name is used to evaluate and output the value of the variable.)
# [kamit@hu Downloads]$ echo $PATH /usr/local/bin:/usr/bin:/bin:/usr/games:/usr/local/sbin:/usr/sbin:/home/finid:/home/finid/bin #
Each entry in PATH is called a directory path. In the output above, for example, /bin, /usr/bin, /sbin are directory paths, and the system looks in each in the order that they are listed for any command you attempt to execute until it finds it.
On a freshly-installed machine, the system assigns an initial set of path names to PATH, so PATH is never undefined. However, you can add and remove path names from PATH, so you’re not stuck with whatever the initial set is. In essence when you set the PATH variable on a modern Linux distribution, you are merely redefining and assigning it a new value. Most users will never ever have to redefine the PATH variable. Only if you’re a developer, you write your own scripts and you add those script to a folder in your home directory, will you ever want to modify the PATH variable to include that folder.
For this tutorial, assume that your username is finid and you have created some really cool Python scripts, which you put in a folder in your home directory. Let’s call that folder pybin. The full path to pybin will then be /home/finid/pybin or $HOME/pybin. HOME is a system variable that points to the path name of your home directory. To see the point I’m making here, type echo $HOME in a shell terminal. The output should match the path of your home directory.
That ends the brief introduction. If you want to learn more about Bash, visit the official documentation page at GNU Bash.
OK, let’s get to the gist of this tutorial – How to set or redefine the PATH variable in Bash.
There are two ways to go about it. One modifies PATH temporarily, while the other modifies it permanently – until you change it again. Both methods are easy, but let’s try the temporary method first.
Set the PATH variable in Bash (temporarily)
When the PATH variable is redefined using this method, the new value will not persist when you log out. I’ll explain the reason further down, but that’s why it’s temporary. To get this done, launch a shell terminal, then just to see what the value of your current PATH is, type echo $PATH.
Now type: PATH=$PATH:$HOME/pybin:. All that does is add $HOME/pybin or /home/finid/pybin to PATH. In other words, PATH has just been redefined.
When you make a change to PATH using this method or any other method, you have to export it to make it an environment variable. That’s how you let the system know the new value of PATH. And you do that using the export command. So to export the change you just made to PATH, type: export PATH.
Now if you type echo $PATH again, you should see the new value of PATH, which should have the directory path you added at the end of the output. As stated earlier, this change will only last until you log out. When next you log in, PATH will revert to its default value.
Why is that?
Any time you log into your machine or server, the system reads an initialization file for the Bash shell located in your home directory. In Linux, that file is called .bash_profile. Notice that the name starts with a . (dot), so it is a hidden file, which means you won’t see it in the output of the ls command. You’ll have to pass ls the -a option to see hidden files.
An initialization file like .bash_profile is a simple shell script, much like any you can write yourself. It is where system variables, like PATH and HOME are defined and assigned. Because it’s just a shell script, you can modify it with any text editor. And that brings us to the next method of redefining the PATH variable in a Bash shell – editing the .bash_profile file.
Set the PATH variable in Bash (permanently)
Note that “permanently” only means that changes made to the definition of PATH in the bash initialization file in your home directory will remain even when you log out. You can always change it again. To edit the .bash_profile, open it with your favorite text editor. For Linux gurus, the most popular text editor is Vi, but if you a new to Linux and don’t want to learn how to use a text editor before you can edit a file, use Nano. So, to open the file, type nano .bash_profile.
This code block shows the contents of that file on my test machine.
# .bash_profile # Get the aliases and functions if [ -f ~/.bashrc ]; then . ~/.bashrc fi # User specific environment and startup programs PATH=$PATH:$HOME/.local/bin:$HOME/bin export PATH #
Pay special attention to the last two lines, because those are the only ones that are pertinent to this tutorial. Did you observe that they are just like the commands used in the first method. They are, right?
However, you only need to modify the PATH assignment line. So to add the same directory path name that we added using the first method, append $HOME/pybin to the end of the PATH assignment line, so that it reads like the one in the code block below. Note that you need to separate it from the entry before it with a colon, so you really need to append :$HOME/pybin, not just $HOME/pybin.
# .bash_profile # Get the aliases and functions if [ -f ~/.bashrc ]; then . ~/.bashrc fi # User specific environment and startup programs PATH=$PATH:$HOME/.local/bin:$HOME/bin:$HOME/pybin export PATH #
When you are done, close the file. Now when you log out and log back in, PATH will assume the new value. But you don’t have to go through the log out/log in routine for the changes to take effect. You can have the system reinitialize the file so that the changes are reflected while you are still logged in. You do that by using the . (dot) command, a command that reads a script file and executes or initializes the commands in it.
That’s all – for now.