Running a shell script in *nix

Bit of a newb using *nix (I'm actually using a bash shell in OS X 10.5) and I wondered what the probably very simple answer is to this...

When I write a script (called, say, my_script) and I've saved it to the current directory, why do I have to put a period and a forward slash in front of its name to run it? Like this:

./my_script

Can't the shell tell that I want to run it from the current directory? Windows seems to handle that situation.

Cheers Ben


The current directory is not automatically in the execution path. This is partly history, and partly a security measure.

Unix is designed as a multi-user system. If your working directory was currently in some folder shared between many users of the system, and your current directory was automatically in the execution path before the system directories, then when you run standard_system_command you might get something evil one of the other users left in that shared folder instead of the standard system command.

Windows did not start its life as a multi-user system, and so the command prompt has different default behaviors.

In my opinion you should not include ./ in your path. If you have created a script you regularly run then you should put it in a shared folder like /usr/local/bin, or in a personal folder like ~/bin which you add to the PATH. You could also put a symlink in one of those folders or create an alias.


"Can't the shell tell that I want to run it from the current directory? Windows seems to handle that situation."

If you added . to your PATH variable, it would, but that's a bad idea from a security perspective. Consider a cracker has created a file called "ls" and a whole bunch of big files in your /tmp directory. You are trying to figure out why /tmp filled up, so you type cd / tmp; ls. Now, ls is running with your privilege level (possibly as root) and can install whatever exploit it wants...probably without your knowledge because the cracker was smart enough to pipe everything to >/dev/null and then run ls at the end with your command line options.


The current directory isn't added to your path automatically by default. The current directory is represented by '.' (just like up a directory is '..'). You can add '.' to your path, but it is not recommended.

Therefore, './my_script' says, run the script 'my_script' that is located in the current directory.