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

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

    On *nix, unlike Windows, the current directory is usually not in your $PATH variable. So the current directory is not searched when executing commands. You don't need ./ for running applications because these applications are in your $PATH; most likely they are in /bin or /usr/bin.

    0 讨论(0)
  • 2020-11-22 04:52

    Your script, when in your home directory will not be found when the shell looks at the $PATH environment variable to find your script.

    The ./ says 'look in the current directory for my script rather than looking at all the directories specified in $PATH'.

    0 讨论(0)
  • 2020-11-22 04:52

    This question already has some awesome answers, but I wanted to add that, if your executable is on the PATH, and you get very different outputs when you run

    ./executable
    

    to the ones you get if you run

    executable
    

    (let's say you run into error messages with the one and not the other), then the problem could be that you have two different versions of the executable on your machine: one on the path, and the other not.

    Check this by running

    which executable

    and

    whereis executable
    

    It fixed my issues...I had three versions of the executable, only one of which was compiled correctly for the environment.

    0 讨论(0)
提交回复
热议问题