Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

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

share|improve this question
1  
Don't ever put . in your $PATH. But since some people will anyway, at least put it at the end where it's at least a little safer than at the beginning (for the same reasons stated in some of the answers). But, really, don't do it at all. –  Dennis Williamson Nov 4 '09 at 20:59

4 Answers 4

up vote 6 down vote accepted

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.

share|improve this answer

"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.

share|improve this answer

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.

share|improve this answer

The ./ tells it to look in the current directory instead of another path first. You can edit your path in your .profile file for your username with instructions here.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.