How to Set Custom $PATH Environment Variable in Linux

In this tutorial, we will cover how you can set your custom $PATH variable in Linux. This works across all Linux distributions, so don’t worry about the distribution you are using. It will work just fine.

Sometimes, you might want to define your own custom $PATH variable which, in most cases, is not provided by your operating system. Doing this will enable you to invoke your variable from any location in the Linux shell without specifying the full path to the variable or command.

Set Custom $PATH Variables in Linux

When you type and run a command on the Linux shell, you are basically telling the shell to run the program. This includes even the basic of commands such as the mkdir, pwd, ls, mv and so many more. Now, your operating system doesn’t shuttle to and fro multiple directories looking to see if there’s a program or executable by that name. These programs are part of an environment variable called $PATH.

The $PATH environment variable tells the shell which directories to find the executable files or programs in response to the commands run by the user. Simple commands such as cp, rm, mkdir, and ls are small programs that have their executable in the /usr/bin directory.

To find the location of the executable program of a shell command, simply run the which command as follows:

$ which command

For instance, to identify the location of the cp command, for example,  execute the command:

$ which cp

Other locations where you can find executable programs include /usr/sbin, /usr/local/bin and /usr/local/sbin

To check what is in your $PATH, run the following echo command:

$ echo $PATH

This displays a list of all the directories – separated by a colon – that store the executable programs, some of which we have just mentioned a while ago.

Echo-path-command-output

Now, we are going to add a custom directory to the $PATH variable.

Setting your own PATH

In this example, we have a shell script called myscript.sh in the scripts directory located in the home directory as shown. This is just a simple script that prints out a greeting when invoked.

ls-command-output-script-folder

To add the script to PATH so that we can call it or execute it no matter which directory we are in, we will use the syntax:

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

Here, we will execute the command:

$ export PATH=$PATH:/home/linuxtechi/scripts

Add-custom-path-export-command

Now, we can execute or run the script from whichever directory on the system simply by typing its name without including the full path to the script.

In the snippet shown, we are running the script in different directory location by simply invoking the script name alone.

Execute-Script-custom-path-linux

Setting custom PATH permanently

When you reboot your system, or start a new terminal, the path you added does not persist. For this reason, it’s best if you set the PATH permanently so that it remains even after restarting the system.

To achieve this, you need to add the export PATH line to the ~/.bashrc  or ~/.bash_profile file.

So, open any of the two files.

$ sudo vim ~/.bashrc

Then add the line shown. Of course, this will vary according to your own individual PATH.

export PATH=$PATH:/home/linuxtechi/scripts

Add-Custom-path-bashrc-linux

Save the file and exit. Then reload the changes using the source command.

$ source ~/.bashrc

Source-bashrc-file-linux

At this point, we have successfully set custom $PATH on Linux. That’s all from this tutorial, your feedback and queries on this is highly welcome.

1 thought on “How to Set Custom $PATH Environment Variable in Linux”

  1. all nice and well, can you add the permanent fix to apply systemwide.
    usually you’d add
    export PATH=$PATH:/your/addition/to/path
    to
    /etc/profile
    or add your own script to
    /etc/profile.d/

Leave a Comment

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