Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Use ./ (Dot Slash) to execute script file?
In Linux, ./ (dot slash) is used to execute script files located in the current directory. The dot (.) represents the current working directory, and the forward slash (/) is the path separator, so ./script.sh tells the shell to run the script from the current location.
What Does Dot Slash Mean?
The dot (.) in Linux represents the current working directory. When combined with the forward slash (/), it creates a relative path that points to files in your current location. For example:
$ ls -l -rwxr-xr-x 1 user1 user1 156 Jun 12 19:09 script.sh -rw-r--r-- 1 user1 user1 245 Jun 12 18:30 data.txt
The forward slash (/) separates directories and files in Unix-like systems, creating a clear path structure.
Why Do We Need ./ To Run a File?
If you try to execute a script without specifying its path, the shell cannot find it:
$ script.sh -bash: script.sh: command not found
Even though the script exists in your current directory, the shell doesn't automatically look there. You must specify the location using ./:
$ ./script.sh The program ran successfully.
This happens because the current directory is not included in the $PATH environment variable by default.
Understanding $PATH Variable
The $PATH variable contains directories where the system searches for executable commands:
$ echo $PATH /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games
When you type a command like ls, the shell searches these directories in order. You can find where a command is located using:
$ which ls /bin/ls
Search Rules
Linux follows specific rules when searching for executables:
If the command contains a slash (/), search only in the specified path, not in
$PATHIf no slash is present, search only in the directories listed in
$PATH
This allows you to have scripts with the same names as system commands without conflicts:
$ ./ls This is my custom ls script. $ ls File1 File2 log script.sh
Security Considerations
Adding the current directory (.) to $PATH creates security risks. If someone places a malicious executable with a common command name in your directory, it could execute instead of the real command:
| Scenario | Risk | Example |
|---|---|---|
| Malicious script named "ls" | Replaces system command | Could delete files instead of listing them |
| Typo commands (e.g., "sl" instead of "ls") | Unexpected execution | Malicious "sl" executable runs |
Windows vs. Linux Behavior
The behavior differs between operating systems:
| Operating System | Current Directory in PATH | Security Impact |
|---|---|---|
| Windows | Yes (by default) | Higher security risk |
| Linux | No (by default) | More secure, requires explicit paths |
Best Practices
Always use
./to execute scripts in the current directoryAvoid adding "." to your
$PATHfor security reasonsUse absolute paths for scripts outside the current directory
Verify script locations with
whichortypecommands
Conclusion
Using ./ to execute scripts is essential in Linux because the current directory is not included in $PATH by default. This design choice enhances security by preventing accidental execution of malicious scripts and requiring explicit specification of script locations.
