Every time you start a shell program like bash, it can initialize a bunch of environment variables that have special significance. “PATH” is one of these variables and it contains the directories where Linux searches for commands when a location for that command isn’t specified. For this tutorial, I’m going to use a test script that I wrote and show you how to add a directory to the PATH variable, so that you don’t have to manually specify the location each time.
Creating a Test Script and Making it Executable
To start off, I’ll create a test script called “printhello.sh” that contains the following basic code:
#!/bin/bash echo "Hello World!"
This script simply prints “Hello World” and exits. I’d written an earlier tutorial on how to make scripts executable. You can see below that while inside the directory that contains the script, I can type:
And it works great. But as soon as I exit the directory and try and just type “printhello.sh”, it says that the command cannot be found. That’s because Linux doesn’t know where to find it! It doesn’t know that it has to search in “/root/mycommands/”. So we need to tell it. And we do that by modifying the PATH variable.
Examining the PATH Variable
You can see the places where Linux currently searches for commands by typing:
This will print a list of directories separated by a colon (:). It can be a bit difficult to read, so I can split it up into different lines like this:
echo $PATH | tr ":" "\n"
This gives the following output:
You can see that it searches in 5 different directories. We can test this out by placing our script in one of them and seeing if it gets executed. So I first create the last directory (if it doesn’t already exist), then move my script into it:
mkdir /root/bin mv printhello.sh /root/bin/printhello.sh
As you can see after I move it, I can just type “printhello.sh” and the command is recognized even though the file no longer exists in the current directory! That’s because Linux found the file in one of the directories inside the PATH variable. So this is one solution – simply place all your scripts inside a directory that already exists in your path. That’s the simplest way to go about it.
But it may not be enough. You might have your own special directory in which you want Linux to search. To do that,, we have to add our custom directory to PATH. So I move the script back to /root/mycommands and continue.
Adding the Directory to PATH Temporarily
To temporarily add your directory to the PATH variable, type the following:
Replace the section in bold with your own directory. As you can see below, executing the above command allows me to execute printhello.sh from wherever I want:
Unfortunately as I mentioned below, this is temporary. Changes to PATH will not persist after a reboot. To make that happens, we modify the hidden file “~/.bash_profile”.
Modifying ~/.bash_profile to Permanently Change PATH
We need to add our directory to the PATH definition in ~/.bash_profile. To do that, open it up using an editor like vi:
Scroll down and you’ll see an existing PATH definition. Add your own directory onto it and separate it from the others with a colon “:” like this:
Save the file and exit. Your changes will take effect automatically on the next reboot. However, you can force Linux to reload the bash_profile file by typing in:
And now your PATH variable would have changed as shown here:
So now you’ve successfully made a permanent change to the PATH variable in Linux, and have added your own custom directory to the list of places that Linux will search when executing a command!
Is your website slow?
Enter its URL below to find out now: