2

If I am in a directory foo, that contains executable bar, why does typing bar results in

bar: command not found

whereas typing ./bar works?

fejese
  • 4,601
  • 4
  • 29
  • 36
  • 1
    possible duplicate of [Why do you need ./ (dot-slash) before script name to run it in bash?](http://stackoverflow.com/questions/6331075/why-do-you-need-dot-slash-before-script-name-to-run-it-in-bash) – fejese Dec 20 '14 at 02:24

3 Answers3

4

Because, by default, the *nix $PATH doesn't include the current directory. Believe it or not, this is a Good Thing:

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

Linux doesn't automatically add current directory to PATH

No, it is not made to be "annoying". It is for security good practice. Imaging you write a script and call it "ls" then save it in a folder where you have access, let's say /tmp. Now if you get your admin to run "ls" in /tmp as root and have "." in the PATH, that will run your script instead of running the real "ls". This way you can do some nasty trick. This is why "." is not in your PATH. Now if you don't need this kind of security, then modify your PATH.

There's nothing that prevents you from modifying your $PATH. On Linux, ".bashrc" in your home directory is a good place. If you add ".", you should add it to the END of your $PATH, not the beginning.

Community
  • 1
  • 1
FoggyDay
  • 11,962
  • 4
  • 34
  • 48
2

In your example, "bar" doesn't work because "./" isn't in your PATH environment variable. You can verify this by typing

echo $PATH
fejese
  • 4,601
  • 4
  • 29
  • 36
argarevarg
  • 31
  • 4
2

Because as opposed to windows, the current directory is not in the $PATH. So your shell won't try to look for a command name bar in the current folder unless you tell it to look there.

fejese
  • 4,601
  • 4
  • 29
  • 36