How to set the PATH variable in Bash

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.)

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.

Related Post:  How to choose an appropriate VPN service

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.

Bash shell terminal
Figure 1: Bash shell terminal on Fedora 20 KDE.

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.

Related Post:  How to build a private corporate blockchain using Hyperledger Fabric

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.

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.

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.

Subscribe to LinuxBSDos.com

Subscribe to receive the latest articles in your Inbox

I agree to have my personal information transfered to MailChimp ( more information )

Trust me, you'll not be spammed...

Please share:

We Recommend These Blockchain Conferences and Servicess

Register now for Blockchain & Decentralized Tech SuperSummit, international conference on blockchain technology in Dallas, TX (USA), April 1 - 4, 2019

Reasons to use control panel for your server

Today's valid web hosting discount codes

Learn how to trade cryptocurrencies using technical and fundamental analyses at BDT SuperSummit

Best binary auto trading software reviews by 7binaryoptions.com

Launch an SSD VPS in Europe, USA, Asia & Australia on Vultr's KVM-based Cloud platform starting at $5:00/month (15 GB SSD, 768 MB of RAM).


  1. in my /root folder i am not able to find .bash_profile .

    share the exact location where it is found

    thank you..

    • Depending on your distribution, it might not even exist. It might just be .profile or something else. Look for all hidden files under your user and root directory and search for all files that have bash or profile in their name.

  2. On another note,

    “How to set the path variable in Bash” is exactly what I typed in.

    SEO for the win.

  3. On Kali’s bash…. my nano .bash_profile is empty.
    What is the full path…. or the correct filename?

    • If it’s empty, then you can customize it. This link will help you understand all the Bash files found on your system or that you can create yourself.

    • In my Kali, I had to open etc/profile and add the path I needed. So go into etc and ‘nano profile’ to open it. I only spent 4 hours and 30 google searches before I gave up and asked someone. So pass this along.

      You will see a piece that looks like this (below). Here, you can see I added /root/Scripts. Now I can run any scripts placed in the newly added path from anywhere.

      if [ “id -u” -eq 0 ]; then
      export PATH

  4. Antondemais

    ¡Great post!

    You can also use /etc/environment to set PATH environment variable, but it does not support variable expansion.

    Extracted from: http://www.sysadmit.com/2016/06/linux-anadir-ruta-al-path.html

  5. Nice and useful tutorial, thank you!

    Also I like the font and colour theme in your screenshots, it really looks beautiful.

  6. Considering the title I’m amazed one can write such lengthy material …

    • Obviously intended for those brand new to our world. The alternative would be to just type the few commands needed to get it done and call it a day. But what good will that do for somebody who’s new to the subject matter?

      • EXACTLY.

        Otherwise, Someone like me will got to Stack Overflow to go ask a few questions relating to path and get BASHED (downvoted) by pros that have spent 20 years in the terminal.

        Thanks for your great tutorial!

Leave a Comment

Your email address will not be published. Required fields are marked *