Why do you need ./ (dot-slash) before executable or script name to run it in bash?

前端 未结 9 929
耶瑟儿~
耶瑟儿~ 2020-11-22 03:59

When running scripts in bash, I have to write ./ in the beginning:

$ ./manage.py syncdb

If I don\'t, I get an error message:

9条回答
  •  栀梦
    栀梦 (楼主)
    2020-11-22 04:37

    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.

提交回复
热议问题