Use ./ (Dot Slash) to execute script file?


Overview

In this tutorial, we will see how to execute a file using the / (dot slash) command in Linux. This is one of the most commonly used commands in Linux and it’s very easy to use. Let us first understand what does the dot slash mean?

What Does Dot Slash Mean In Linux Commands?

The dot slash or the forward slash (/), as you may know, is used to separate directories from files. For example: If I have a directory called “/home/user1/Desktop” then when I type ls -l on that directory, it would show me all the contents of that directory like below −

-rwxr-xr-x 1 user1 user1 0 Jun 12 19:09 Desktop

If I want to list only the files inside that directory, I can do so by typing ls –l /home/user1/desktop. The above command will give me the same output as shown below −

drwxr-xr-xs 2 user1 user1 4096 Jun 12 19:09 desktop

Why Do We Need ./ To Run a File?

We're going to use the command line to execute the script.sh file. We won't mention any paths.

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

Since we're in the same directory as script.sh, Bash was able to find the script.sh file. However, since the script.sh file doesn't exist, we must tell Bash where to look for the script.sh file by specifying its location.

When using Linux, the. (dot) symbol indicates the current working folder. Now let's say we want to run our script from within the current working folder.

$ ./script.sh
The program run successfully.

Note the forward slashes (/). Forward slashes are used to denote directories in Unix/Linux systems. They're also used to separate filenames from each other. In our case, they're used to separate the script filename from the.sh extension. Without them, we'd end up with a file named "./script.sh" instead.

Now it's time to answer our previous questions. Why do we want/require to add our current directory to $PATH? Because our current directory isn't included in $PATH.

System Executables vs. User Executables

The $PATH variable typically contains a path for each directory where programs reside. Let’s take a look at its typical contents −

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

$PATH is used to specify where the system commands (like ls) are located. On Linux systems, the shell searches for the command in the directories listed in the $PATH environment variable. If the command isn't found there, then the shell tries to run the command from the current directory.

$ ls
File1 File2 log

If there are multiple paths containing an executable, Linux will choose the first one found in $PATH.

You can also use which to obtain the full path of any program that can be run by typing its name at the shell prompt.

$ which ls
/bin/ls

Search Rule

When searching for executable files in Linux, there is a command called which that helps us remember one important thing −

  • If the slash (/) character is present in the file name, don't search for the file in $PATH.

  • If the slash is NOT in the PATH variable, then search only within the current directory.

We can have scripts with the exact same names of commands, for example, and execute them in the current directory with no conflicts.

For example, let's say we want to write a shell command called "ls" which lists files in our current directory. We could then type the following into our terminal window −

$ ./ls

This is a sample script for ls.

Because we specify "/" before the filename, Bash will search for the specified filename in the current working directory.

You can also use the system command "ls" to list all files and folders in the current folder.

$ ls
File1 File2 log ls

If we run the command without any arguments, Bash searches for files in the directories listed in the $PATH environment variable.

You can add the current working dir (“.“) to the $PATH environment variable by exporting it, but it’s best to avoid doing so because it may lead to security issues.

Security Issue When Including . in $PATH

Suppose we add the current working directory to $PATH. If we put "." as the first entry in PATH, then our ls sample will be executed instead of the real ls command.

$ ls

This is a sample script for ls.

If we run ls −l, we want to be sure that we're really running the ls command, and not some other executable called ls. Someone could put malicious code into an ls executable to delete our file or change its contents. So this is a potential security threat.

We can add "." to our $PATHS variables so that the system commands `ls` and `grep` will both work. However we may still run into some security issues.

If we type "sl" into our terminal, instead than receiving an "illegal command" or "command not found" response, there may be a malicious program called "sl" in the current working folder that we're unaware of.

$ sl

This could be a virus.

Windows vs. Linux

By default, the Windows PATH variable holds the current directory. So, when you run a program from the Windows console, it looks for programs in the current directory before looking elsewhere.

Unlike Windows, where the current directory is automatically added to the system's $PATHS variable, in Linux, the current directory isn't automatically added to the system’s $PATHS variable. To include the current directory in the system’s environment variables, we must manually add it to the system’S $PATHS variable using the export command.

Conclusion

We learned to use "./" for executing shell commands in the current working dir. Because the current directory isn't in the $PATHS variable, we must either specify the relative or absolute paths to the files. We then went through the search rule in Linux, which explains how to use grep command to search for We discussed some security risks that might occur if we added "." to the $PATH environment variable.

Finally, it‘s always better to explicitly specify where we want to run our executable files. It’d be a good idea to check whether the PATH environment variable includes “.” when running multiple user accounts.

Updated on: 26-Dec-2022

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements