1. Overview

In this tutorial, we’ll learn when and how to use “./” for running a script in Linux. We’ll also explain how the path search works in Linux in general and Bash in particular.

2. Why Do We Need ./ To Run a File?

Let’s assume we want to run the script.sh file that is an executable and lives in the current directory. Let’s run this executable file without mentioning any path:

$ script.sh
-bash: script.sh: command not found

Although we are in the same directory as the script.sh file, Bash could not find this file. Therefore, we need to specify the relative or absolute path to the file so that shell knows where our executable file is.

In Linux, the dot character (.) denotes the current directory. Let’s now call our script including it as the path to the file:

$ ./script.sh
The program run successfully.

Note the slash (/) after “.”. Slash is the path separator in Linux. We use it to separate the current directory (.) from the file name. Without it, we have .script.sh that denotes a hidden file instead.

It’s now time to answer our previous question. Why do we need ./? Because our current directory is not included in $PATH.

3. System Executables vs. User Executables

The $PATH variable is an environment variable that contains a list of paths. Let’s look at its typical content:

$ echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/games:/usr/games

$PATH contains the path to the system commands like ls so we don’t need to include the path to them in every execution. Linux will search for the executable in the paths specified in $PATH and runs the file or let us know if couldn’t find it:

$ ls
File1   File2   log  

Note that if two paths contain the desired executable, Linux will use the first path that appears in $PATH.

Also, We can use which command to get the full path of any command that can be found using $PATH:

$ which ls
/bin/ls

4. Search Rule

There is a rule in Linux Bash when looking for executables we need to remember:

  • If the slash character is present in the pathname then don’t search in $PATH
  • If the slash is not in the pathname then search only in $PATH

This means we can have scripts with the same name of a system command, for example, and run them in the current directory without any conflict.

For example, we can create a script called ls in our current directory and run it as below:

$ ./ls
This is a sample script for ls.

Note that because we specify “/” before the file name, Bash will search in the path we specify. In this case the current directory.

We can also run the system command ls, which lists all file and folders in the current directory:

$ ls
File1   File2   log   ls

In this case, Bash will search in the paths contained in the $PATH variable because / is not in the pathname. Note that the ls file in the result is our previous sample script.

We can add the current directory (“.”) to the $PATH variable using export but it’s not recommended. As “.” varies upon the directories we are in, it can cause security issues.

5. Security Issue When Including . in $PATH

Let’s assume we add the current directory to $PATH. If we put “.” as the first path in $PATH, our ls sample file will run instead of the external command ls:

$ ls 
This is a sample script for ls.

If we run ls on a system, we want to be sure we’re running the system ls, not another user’s executable file with the name ls. Someone can put malicious code in this ls file to remove or modify our files. Therefore, this is a security issue.

We can put “.” as the last path in $PATH. In this case, the system command ls will run. The previous problem is solved but the security issue may still be there.

For instance, if we have a typo like sl in the terminal, instead of getting an error, there may be a virus in the current directory with the name sl that we’re not aware of:

$ sl 
This could be a virus.

6. Windows vs. Linux

The Windows PATH variable contains the current directory by default. So, the Windows command prompt (cmd) searches first in the current directory and then in external commands.

Although it is very common to execute programs in the current directory, Bash won’t search in the files that exist in the current directory and we need to explicitly specify the path to the file. So, unlike Windows, and because of the potential issues we stated above, the current directory is not included in Linux $PATH by default.

7. Conclusion

In this article, we learned to use ./ for running scripts in the current directory. As the current directory is not in the $PATH variable, we need to specify the relative or absolute path to the file. Then, we explained the search rule in Linux. In the end, we talked about some security issues that could happen if we add “.” to the $PATH variable.

Finally, it’s always better to explicitly include the path for running our executables. Also, it’s a good security measure to check the $PATH variable in multi-user systems doesn’t include “.” in $PATH.

Comments are closed on this article!