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 ./
)
This is where the dot slash ./ notation comes in. It means “Look in the current directory.” When you use ./, you tell Ubuntu or Fedora or SUSE or whatever Linux distribution you're using to look in the current directory for the command you wish to run, and completely ignore what's on the application PATH.
A dot slash is a dot followed immediately by a forward slash ( ./ ). It is used in Linux and Unix to execute a compiled program in the current directory.
The dot command ( . ), aka full stop or period, is a command used to evaluate commands in the current execution context. In Bash, the source command is synonym to the dot command ( . ) and you can also pass parameters to the command, beware, this deviate from the POSIX specification.
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 PATH
variable. 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 sl
instead of ls
. If the current directory is in PATH
, the shell will try to execute the sl
program in that directory (since there is no other sl
program). That sl
program 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 sl
part was just an example. The directories in PATH
are searched sequentially and when a match is made that program is executed. So, depending on how PATH
looks, typing a normal command may or may not be enough to run the program in the current directory.
When bash interprets the command line, it looks for commands in locations described in the environment variable $PATH
. To see it type:
echo $PATH
You will have some paths separated by colons. As you will see the current path .
is usually not in $PATH
. So Bash cannot find your command if it is in the current directory. You can change it by having:
PATH=$PATH:.
This line adds the current directory in $PATH
so you can do:
manage.py syncdb
It is not recommended as it has security issue, plus you can have weird behaviours, as .
varies upon the directory you are in :)
Avoid:
PATH=.:$PATH
As you can “mask” some standard command and open the door to security breach :)
Just my two cents.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With