When running scripts in bash, I have to write ./ in the beginning:
$ ./manage.py syncdb
If I don’t, I get an error message:
$ manage.py syncdb
-bash: manage.py: command not found
What is the reason for this? I thought . is an alias for current folder, and therefore these two calls should be equivalent.
I also don’t understand why I don’t need ./ when running applications, such as:
user:/home/user$ cd /usr/bin
user:/usr/bin$ git
(which runs without ./)
Because on Unix, usually, the current directory is not in
$PATH.When you type a command the shell looks up a list of directories, as specified by the
PATHvariable. The current directory is not in that list.The reason for not having the current directory on that list is security.
Let’s say you’re root and go into another user’s directory and type
slinstead ofls. If the current directory is inPATH, the shell will try to execute theslprogram in that directory (since there is no otherslprogram). Thatslprogram might be malicious.It works with
./because POSIX specifies that a command name that contain a/will be used as a filename directly, suppressing a search in$PATH. You could have used full path for the exact same effect, but./is shorter and easier to write.EDIT
That
slpart was just an example. The directories inPATHare searched sequentially and when a match is made that program is executed. So, depending on howPATHlooks, typing a normal command may or may not be enough to run the program in the current directory.