npressfetimg-5809.png

Why must you use ./ to run your Ubuntu scripts? The meaning of Linux’s dot slash explained. – TheServerSide.com

Why use dot slash for commands?

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.

The Unix ./ script syntax mystery can be explained in three parts:

  1. The role of the application PATH in Linux
  2. The ./ notation which denotes the current directory
  3. The need to avoid conflicts with existing commands

dot slash vs the Unix PATH

Every computer has an application PATH. The PATH is a collection of folders in which the OS will look whenever an executable command or runnable script is invoked.

All of the built-in commands your familiar with, such as ls, cat, echo, touch and chmod are all 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 not likely saved in a folder that’s part of the application PATH. Which means if you run your custom script like you would a command that resides on the PATH, the Linux OS wouldn’t be able to find it.

Meaning of ./ in Ubuntu

This is where the dot slash ./ notation comes in.  ‘Look in the current directory’ is what dot slash ./ means. When you use ./, you tell Ubuntu or Fedora or Suse or whatever Linix 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 your script or program will be found and executed faster if the OS doesn’t have to search the entire application PATH first before it’s found locally. Although searching the PATH on Ubuntu or Suse really shouldn’t take much time at all. The real benefit to the use of ./ to run scripts in Linux is that it avoids naming conflicts amongst commands.

Benefits of dot slash notation

Disambiguation and the elimination of naming conflicts is the key benefit to the need to use ./ to run Unix commands.

For example, imagine 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, my command would never get found. Instead, the script named javadoc.sh which is part of every JDK bin directory would be found first. My script would never run.

And that’s why we always use /. to run our scripts in Unix and Linux environments.

Use the dot slash to run scripts you have permissions to execute.

Word of note on running scripts

And just a word of note 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’ll need to explicitly give yourself execute permissions to that folder, otherwise your attempt to run your command will be met with an ‘operation forbidden’ message, regardless of whether you use the Unix dot slash ./ command notation properly or not.

 

 

 

 

Source: https://www.theserverside.com/blog/Coffee-Talk-Java-News-Stories-and-Opinions/Why-must-you-use-to-run-your-Ubuntu-scripts-The-meaning-of-Linuxs-dot-slash-explained

Related Posts