You need it because the current directory is not in PATH. Linux/Unix always looks in the $PATH environment variable. Windows already has a behavior: if it is not on PATH, looks in the current directory, so you don’t need to put the directory before the executable.
Windows does the search in a different way. After separating the command from the arguments and solving the environment variables, the following steps are taken:
- if a directory is not passed, find if it is an internal shell command. If you find one, run the command. Otherwise, go to 2;
- if a directory is passed, the directory executable will be executed. Otherwise, it will return error to the user;
- if a directory is not passed, the current directory command will be executed;
- finally, will be sought in the
%PATH%
.
source: Technet Windows NT-
Command Search Sequence
If it bothers you on Linux/Unix, just add PATH=$PATH:.
in the file that initializes your environment variables. And you will never need to ./
to run executables.
I’ll leave the file comment /etc/profile.d/z-dot-in-non-root-path.sh
from Slackware Current, because there’s an interesting explanation for this.
#!/bin/sh
# Traditionally Slackware has included '.' at the end of the non-root
# $PATH, and kept this behavior long after it had been dropped elsewhere
# due to the relatively low attack risk by having it at (or near) the
# end of the $PATH. But times have changed, and having this as a default
# violates POLA (principle of least astonishment) just like removing it
# back in the early 90s would have. So, by default this script is not
# enabled. If you'd like '.' back at the end of your $PATH for non-root
# users systemwide, make this script executable. A better choice is
# probably to leave it off and let individual users decide to add it
# in their local profile scripts if they want it. Even better is just
# to start programs in '.' with ./program, like most of us have been
# doing for years.
# For non-root users, add the current directory to the search path:
if [ ! "`id -u`" = "0" ]; then
PATH="$PATH:."
fi
In short, this practice violates the POLA in the vision of Slackware developers.
On the way :) On the second point of the question, do you have anything to say? Or has MS left a glitch unintentionally and is keeping it by backward compatibility? XD
– Jefferson Quesado
I think windows does the "search" in the current directory and only then will check the system variable "PATH". But since I never liked the windows shell, I never really cared much, but I’m curious to know if there is any specific reason.
– lazyFox
It is by compatibility with DOS, which at the times of the floppy disk had no variable
%PATH%
— without hard drives, it makes no sense to establish a list of directories to search for executables. Semantics was inherited from version to version until we got here.– Wtrmute
Interesting view of safety
– gfleck