Why do I need './' to run commands on Unix?

6

I noticed that every executable that I need I need by a ./ to run it on both Linux and MacOS (Unix / Unix-like systems). For example, to compile a C file with GCC and run it right away, I make the following commands:

$ gcc fonte.c
$ ./a.out

This also applies when I create a shell script:

$ vim script.sh
$ chmod u+x script.sh
$ ./script.sh

On the other hand, when I do it in Windows, I do not need to ./ up front. For example, if I create a bat , in the exemplo folder, just call cmd and execute the script directly:

$ cd exemplo
$ script.bat

Even when I use MingW (as git bash provided by SourceTree), which is a more "uniched" environment within Windows, I do not need ./ either. Taking the same example of the shell script generation in Linux, git bash would look like this:

$ vim script.sh
$ script.sh

Questions

  • Why on more Unix systems do I need this ./ to run my executables in the current working directory?
  • Why in Windows do not I put ./ ? (Not the .\ equivalent in the Windows directory separator notation?)
  • asked by anonymous 30.06.2017 / 11:48

    2 answers

    5

    You need because the current directory is not in PATH . Linux / Unix always looks for the $ PATH environment variable. Windows already has a behavior: If it is not in PATH , look in the current directory, so you do not need to put the directory before the executable.

    30.06.2017 / 14:44
    5

    This "./" is first placed behind the name of your executable to ensure that you are running the same within the current directory.

    Secondly, this is a security method to prevent you from running any harmful program / script that has been lodged in the current directory. If there was no need to put "./" it would be possible to put malicious scripts inside a directory with the following names "ls" "nano" "vim" "ps" among others, and when executing "ps" for example could to be running a completely different command from which it would not know its output.

    I think that's basically it, but I'm waiting for more answers.

        
    30.06.2017 / 12:49