Learn How to Set Your $PATH Variables Permanently in Linux

In Linux (also UNIX) $PATH is environment variable, used to tell the shell where to look for executable files. $PATH variable provides great flexibility and security to the Linux systems and it is definitely safe to say that it is one of the most important environment variables.

Don’t Miss: How to Set and Unset Local, User and System Wide Environment Variables

Programs/scripts that are located within the $PATH’s directory, can be executed directly in your shell, without specifying the full path to them. In this tutorial you are going to learn how to set $PATH variable globally and locally.

First, let’s see your current $PATH’s value. Open a terminal and issue the following command:

$ echo $PATH

The result should be something like this:

/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games

The result shows a list of directories separated by colons. You can easily add more directories by editing your user’s shell profile file.

In different shells this can be:

  1. Bash shell -> ~/.bash_profile, ~/.bashrc or profile
  2. Korn Shell -> ~/.kshrc or .profile
  3. Z shell -> ~/.zshrc  or .zprofile

Please note that depending on how you are logging to the system in question, different file might be read. Here is what the bash manual says, keep in mind that the files are similar for other shells:

/bin/bash
The bash executable
/etc/profile
The systemwide initialization file, executed for login shells
~/.bash_profile
The personal initialization file, executed for login shells
~/.bashrc
The individual per-interactive-shell startup file
~/.bash_logout
The individual login shell cleanup file, executed when a login shell exits
~/.inputrc
Individual readline initialization file|

Considering the above, you can add more directories to the $PATH variable by adding the following line to the corresponding file that you will be using:

$ export PATH=$PATH:/path/to/newdir

Of course in the above example, you should change “/path/to/newdir” with the exact path that you wish to set. Once you have modified your .*rc or .*_profile file you will need to call it again using the “source” command.

For example in bash you can do this:

$ source ~/.bashrc

Below, you can see an example of mine $PATH environment on a local computer:

marin@[TecMint]:[/home/marin] $ echo $PATH

/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/home/marin/bin

This is actually a good practice to create a local “bin” folder for users where they can place their executable files. Each user will have its separate folder to store his contents. This is also a good measure to keep your system secured.

If you have any questions or difficulties setting your $PATH environment variable, please do not hesitate to submit your questions in the comment section below.

Marin Todorov
I am a bachelor in computer science and a Linux Foundation Certified System Administrator. Currently working as a Senior Technical support in the hosting industry. In my free time I like testing new software and inline skating.

Each tutorial at TecMint is created by a team of experienced Linux system administrators so that it meets our high-quality standards.

Join the TecMint Weekly Newsletter (More Than 156,129 Linux Enthusiasts Have Subscribed)
Was this article helpful? Please add a comment or buy me a coffee to show your appreciation.

17 thoughts on “Learn How to Set Your $PATH Variables Permanently in Linux”

  1. Here’s my solution, after reading a lot of conflicting advice. Some people say put $PATH changes in .bash_profile (which doesn’t run for a bash shell run from the desktop). Some say put them .bashrc (which does run, probably more than once).

    If I simply add a line in .bashrc (I mean /home/roopid/.bashrc):

    export PATH="$PATH:/home/roopid/source/rexx:."
    

    Then it gets added twice, which has caused problems when "." is not the last item (I never found out why).

    So I use this, which checks whether my changes have already been made:

    if ! [[ "$PATH" =~ "source/rexx" ]]
    then
        PATH="$PATH:$HOME/source/rexx:."
    fi
    

    It adds 2 directories, one for Rexx scripts I use for admin tasks, and "." because I like to include the current directory for commands.

    Whole file included below for completeness:-

    # .bashrc
    
    # Source global definitions
    if [ -f /etc/bashrc ]; then
    	. /etc/bashrc
    fi
    
    # User specific environment
    if ! [[ "$PATH" =~ "$HOME/.local/bin:$HOME/bin:" ]]
    then
        PATH="$HOME/.local/bin:$HOME/bin:$PATH"
    fi
    
    # Check changes by me (roopid) have not already been made
    if ! [[ "$PATH" =~ "source/rexx" ]]
    then
        PATH="$PATH:$HOME/source/rexx:."
    fi
    
    
    export PATH
    
    # Uncomment the following line if you don't like systemctl's auto-paging feature:
    # export SYSTEMD_PAGER=
    
    # User specific aliases and functions
    if [ -d ~/.bashrc.d ]; then
    	for rc in ~/.bashrc.d/*; do
    		if [ -f "$rc" ]; then
    			. "$rc"
    		fi
    	done
    fi
    
    unset rc
    
    Reply
  2. @Ryan

    When setting up the Android & Java SDKs on this system I had the same issue when defining the needed PATH variables and adding the library paths. I dug around and found that my current system has both ~/.bashrc and /etc/profile and that no matter what I tried defining them in ~/.bashrc & it wasn’t working, but commenting the lines out in ~/.bashrc and copying them to /etc/profile did exactly as expected.

    I came to the conclusion that perhaps there’s an overwrite happening, but I’ve never had a chance to understand it further.

    Reply
  3. Hello,

    I am trying to change my $PATH and i did a vi on .bash_profile where i added this command:

    export PATH=$PATH:/HOME/scripts
    

    Then I saved the file and then i did a source ~/.bashrc, but my scripts file did not show up when i ran this command.

    $ echo $PATH
    

    WHAT did i do wrong? Respectfully request your help.

    Thank you.
    cajunchief

    Reply
  4. Hi,

    My system is Fedora 28 and the default shell is tcsh. I want to know how can I set permanent PATH? It is necessary to say that I do not have root access.

    Before, I had Ubuntu, the default shell was BASH. So I used to edit .bashrc file.

    Thanks,
    Shima

    Reply
  5. Hello and good days.

    I need help what happened if i run virtualenv in my environment path, what i mean is “chk@waklu:$ virtualenv” did the path environment is change on bashrc, profile, or even in the /etc/bash, /etc/profile.

    The problem is, I cannot detect my python and when i try to echo path should get /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/home/marin/bin
    but I am getting blank, not showing up. Need help I run 16.04 Ubuntu.

    Reply
  6. Frustrating, because there’s very incomplete information here. “In this tutorial you are going to learn how to set $PATH variable globally and locally.” Really? I’m going to set the path for the whole world? What do you -really- mean by globally? (For all users, I’m assuming)

    “First, let’s see your current $PATH’s value. Open a terminal and issue the following command: echo $PATH
    Why can’t I just type “$PATH“? Seems to do the same thing.

    I don’t see anything about path in my .bashrc file.

    # cat .bashrc | grep PATH 
    

    “$ export PATH=$PATH:/path/to/newdir”

    What if I make an unnoticed typo; how do I fix that?

    Reply
  7. Whenever I open the terminal in Linux, I get a new blank page, but to continue from where I had stopped, what should I do?

    Reply
  8. Hi Marin,

    This works. But I need this to work automatically every-time my machine boots up. Now I have to run the source ~/.profile every time I restart. Is there any way to do this automatically?

    Reply
  9. I have vxargs.py script in directory “/home/csgrads/kumar392/RESILIO“. However when I try to add the directory “/home/csgrads/kumar392/RESILIO” to the path it gives an error – Bad : modifier in $ (/).

    I used the command mentioned by you, i.e. export PATH=$PATH:/home/csgrads/kumar392/RESILIO.

    Can you please help.

    Reply
  10. Great post!

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

    Reply
    • Thank you “tomatreides” for this comment. It just solved a problem I have had more more than one week.

      We had this odd problem on a CentOS 7.3.1611 box where we installed Java 1.7.0_80, not upgraded or updated Java but a fresh install. What we noticed was that the PATH seems to have been ruined by something, but we could not figure out by what or how. You just solved that!

      Our theory is that the Java installation wrongfully puts its stuff in the “/etc/environment”-file and that then completely ruins the PATH variable since the PATH then contain “$PATH” and “$JAVA_HOME” instead of its expanded content.

      The fix is to move the Java related content from the “environment” file into a new file in the folder: “/etc/profile.d/”. Using the files “/etc/profile” or “/etc/bashrc” or any other system wide file is not recommended. Using e.g. “~/.bash_profile” or “~/.bashrc” or any other user specific file is also not recommended. The files in “/etc/profile.d/” are also not affected if you update the OS. So this is imho where this type of things should have been done in the first place (Oracle, are you listening?).

      Maybe there is a better way to solve this, but this works for us. I hope this helps anyone else who have the same problem. If you want to know more please reply to this comment and I will be more than happy to help you.

      Reply
  11. Nice article .. If we need to use java on ubuntu then what are the parameters we should use? or on which file.
    Any help will be appreciated!!

    Reply
  12. Great post and great blog!!!
    In my PATH I have some variables repeated, e.g., /usr/local/bin

    I’ve never had issues with that, so I think that it should not be so important to have duplicates, am I wrong?

    Reply
    • The path is resolved with the first match. If you have identically named executables then the specific order of the paths are important and you might find unexpected behavior when there are duplicate or out of sequence entries.

      Reply

Got something to say? Join the discussion.

Thank you for taking the time to share your thoughts with us. We appreciate your decision to leave a comment and value your contribution to the discussion. It's important to note that we moderate all comments in accordance with our comment policy to ensure a respectful and constructive conversation.

Rest assured that your email address will remain private and will not be published or shared with anyone. We prioritize the privacy and security of our users.