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

前端 未结 9 1042
耶瑟儿~
耶瑟儿~ 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

    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.

提交回复
热议问题