why ./ is needed to run a runnable file?

Solution 1:

To execute a script, you need to type in the full path to the script unless the script is present in one of the directories listed in your $PATH environment variable. Generally (and by default) . (the current directory) isn't in your $PATH, so you need to type ./script to execute it.

Solution 2:

Because the current directory is not in your path. This is a safety feature. If it was in your path, someone could potentially drop a malicious copy of a common command, and when you are in that directory instead of running the real sudo, for example, you'd run the fake one. That'd be a bad thing.

Solution 3:

echo $PATH

You must have the current directory (a single dot) in your path for this to work.

You can add it to your path if you want with the following commands.

sh/bash: export PATH=$PATH:.

tcsh/csh: set PATH = ($PATH .)

Solution 4:

the shell searches the path for executables, and by default ./ is not in the path.