Why we use ./ (dot slash) to run Linux scripts?
When you run your own executable command or shell script on Linux, you must prepend ./ to the Unix command. But why?
Why must you use a dot slash to run commands in Unix? You don’t have to do that in Windows with a batch file.
Why use ./ dot slash in Unix?
The Unix ./ script syntax mystery can be explained in three parts:
- The role of the application PATH in Linux
- The ./ notation which denotes the current directory
- The need to avoid conflicts with existing commands
Unix’s dot slash vs the PATH
Every computer has an application PATH. This is a collection of folders in which the OS looks when an executable command or runnable script is invoked.
All of the built-in commands you are familiar with, such as ls, cat, echo, touch and chmod, are on the application PATH. Users often put the commonly used programs in the JDK or Git on the application PATH as well.
The problem is, if you’re trying to invoke a script your wrote yourself, it’s likely not saved in a folder that’s part of the application PATH. This means if you run your custom script like you would a command that resides on the PATH, the Linux OS won’t find it.
Meaning of ./ in Linux
This is where the dot slash ./ notation comes in. It means “Look in the current directory.” When you use ./, you tell Ubuntu or Fedora or SUSE or whatever Linux distribution you’re using to look in the current directory for the command you wish to run, and completely ignore what’s on the application PATH.
One benefit of the dot slash notation is that the OS will find and execute your script or program more quickly, because the OS doesn’t search the entire application PATH first. On the other hand, it shouldn’t take much time at all to search the PATH on Ubuntu or SUSE.
The real benefit to the use of ./ to run scripts in Linux is that it avoids naming conflicts amongst commands.
Benefits of dot slash syntax
Disambiguation and the elimination of naming conflicts are the key benefits to using ./ to run Unix commands.
For example, I create a script named javadoc.sh to generate documentation for my RESTful web applications. If an administrative user had added the \bin directory of the JDK to the application PATH, the OS would never find my command. Instead, it would first find the script named javadoc.sh which is part of every JDK \bin directory. My script would never run.
That’s why we always use ./ to run our scripts in Unix and Linux environments.
A word about permissions and running scripts
One last piece of advice, for anyone who is having trouble running a script or program of their own.
When you create a folder in a Linux distribution like Ubuntu, you only have read and write permissions, even though you are the folder owner. You need to explicitly give yourself execute permissions to that folder. Otherwise, when you attempt to run your command you will encounter an operation forbidden message — regardless of whether you use the Unix dot slash ./ command notation properly or not.