When you execute a command in Linux, the Operating System needs to know where to find it. This isn’t unusual since even the Windows CLI has the same requirement. In Windows, the list of locations where the OS searches from the command is specified by the “ENVIRONMENT” variable. In Linux, this is called the “PATH” variable instead.
There is one difference though, is that Windows automatically searches the current directory for the command, whereas Linux does not. In this article, I’ll show you how to permanently add a location to Linux’s PATH variable via a simple example.
Test Script in Local Directory
To demonstrate this, I create a test script that simply prints “Hello World” to the screen. After creating the script, I gave it the appropriate “755” permissions etc, so I won’t cover that here. The problem arises, when I try and execute the script like this:
As you can see, the “testscript” file is sitting right in my local directory, but I’m still getting a “command not found” error when I try and run it. This is because I haven’t told Linux to specifically search my current local directory for the command. I can do this by typing this:
And then I get the expected result:
This time, the command executes and prints like I want it to. If you want to be able to execute scripts in the local directory without the additional hassle of typing “./”, you need to add the dot (.) to the PATH variable.
Why Isn’t the Dot (.) Included by Default?
A quick note as to why Linux behaves in this strange manner. It’s for security reasons. The danger is that someone could create a new version of a common command like “ls” and put it in your local directory. And if you just use “ls”, there is a danger that the malicious version will be used instead of the system default. For this reason, we’re going to add the current directory to the end of the PATH variable instead of the beginning. This way, system commands will be executed first.
The PATH variable is located in ~/.bash_profile. To edit it, we’ll be using the native editor “vi”. I’d earlier written a tutorial on getting started with vi, so you can read that if you need to brush up quickly. We edit the file by typing:
Inside, you’ll see one of the lines starting with “PATH”. What we want, is to add the following to the end of the line:
Save your changes and exit. Now the PATH variable will also search the current directory if the command isn’t found in any of the earlier locations. As seen above, this is for security reasons.
Unfortunately, your command still won’t work because the changes haven’t taken effect yet. To force the changes to go through, run the following:
And now, the command works as expected:
Replacing “.” With Your Directory
This article was to show you how to add any location to the PATH variable. To do this, simply replace the current directory represented by a dot (.) with your fully qualified path. Reload the bash profile as shown above, and you’re done!
Of course, another approach is to simply move your custom commands to one of the existing locations in the PATH variable. You can see above, that two default locations are:
Just move your custom scripts over to one of these locations and you can run them from the command line without needing to add anything to your PATH variables!
I’m a NameHero team member, and an expert on WordPress and web hosting. I’ve been in this industry since 2008. I’ve also developed apps on Android and have written extensive tutorials on managing Linux servers. You can contact me on my website WP-Tweaks.com!