Adding executable to your PATH variable

In this tutorial, we will learn how to add our shell scripts to the PATH variable so that we can access our scripts from anywhere in the command line. This is a critical feature to our scripts so that we are no longer restricted to the directory that the script is contained in.

What is the PATH variable?

The PATH variable is an environment variable containing an ordered list of paths that your Linux will search for executable's when running a command. The advantage is such that we don't have to specify any absolute or relative paths while running a command or script. Sounds cool, right.

echo "Hello World"

For example, whenever you run a command such as the echo or the ls in your terminal, you are never specifying the location of where these executable's are located. But still, you get the desired output. This is because all these commands are located in the /bin/* folder and the path of that bin directory is specified in the path variable.

So how many PATH variables are currently in my system?

You can view all your current PATH variables by echoing out the PATH variable itself

$ echo $PATH

image.png As you can see, these are the current directories in my PATH variable. If you see anything different than the one's in mine, it's totally ok.

If you look carefully, you can see the bin directory in the list. This ensure's I can access all the executables inside the bin directory without specifying the path to the bin directory.

Adding a new path in BASH for our shell scripts

When adding a new path, we want the change to be persistent. That is, every time we boot up our system, we should be able to access the scripts that we added to the PATH variable. For this, we will use the export command to export our new path and we will make these changes in the .profile file in the home directory so that the changes are persistent.

In our case, all our shell scripts are in a particular directory and we will add that directory to the path variable. Make sure to replace the name of my directory with your directory.

Step 1: Move to the home directory

$ cd ~

Step 2: Open the .profile file

$ vim ~/.profile

Step 3: Add the following line to the end of the .profile file (save and exit the file)

export PATH="$PATH:$HOME/bash-scripting"

Step 4: Either restart the terminal or use source command to reload the .profile file

$ source ~/.profile

The .profile file is always read when our shell boots up. So, the export command gets run everytime when the shell is loaded and all our scripts are always available to us.

Step 5: Verify our folder is in path variable using echo command

$ echo $PATH

Step 6: Now run your scripts in the folder that you added to PATH varible

$ backup.sh

My script name is backup.sh and it runs perfectly.

image.png Yes, our folder that contains the script is in the path variable.

Summary

In this tutorial, you learned to add scripts or any other executables to the PATH environment variable. This helps you to launch scripts from anywhere in your scripts without the pain of specifying the absolute path to the script.