Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

uaecasher

macrumors 65816
Original poster
Jan 29, 2009
1,289
0
Stillwater, OK
hello,

I was writing a simple hello world program in C, i have compiling the program in terminal using the code

$ gcc project.c

i got the file a.out

now i want to run it i use the code

$ a.out

but i get this error:

-bash: a.out: command not found


here is the source code for hello world program:

Code:
#include <stdio.h> 
int main (void) 
{ 
	printf ("Programming is fun.\n"); 
	return 0; 
}
 
Try this:

Code:
./a.out

The current directory normally isn't in the path for security reasons. Imagine if someone created a virus and named the executable "ls".
 
You gave no full path for the file you wanted to run, so the system searched through the directories in your PATH environment variable. The current directory, referred to as ., is not in PATH by default for security reasons. This means that if you want to run a program that is in your current directory, you need to give an explicit path.
./a.out
Is a path to the file a.out in the current directory.

-Lee
 
To make "security reasons" a bit more concrete, the idea is that if the current directory would be in the path, there could be malicious programs in certain directories which would have the name of a popular unix command, but misspelled (for instance, "mroe" instead of "more"). I you happened to be in that directory and you'd type in "mroe", instead of getting "-bash: mroe: command not found", the malicious program would run.

Even worse would be to have the current working directory as one of the first directories in the path, since even correctly spelled unix commands could be "hijacked" that way.
 
On UNIX, every command is a file somewhere. This is different on DOS/Windows, where some commands (like DIR) are built in to the shell. When you type a command on UNIX, the system has to know where to look for the file. Try typing this in the terminal:

Code:
echo $PATH

You will get a list of directories where the system will look if you type a command without a full path.

Code:
/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/X11/bin

For example, when you type "ls", it will try "/usr/bin/ls" and find that that file does not exist. Then it will try "/bin/ls" and find that that file does exist, so it will run it.

The current directory is always represented by the dot. Since there isn't a "." in the $PATH variable, the system doesn't know where to find your file. The reason that there is not "." in the $PATH is that you always want to know exactly what command you are running. Consider what would happen if you set your path to this:

Code:
.:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/X11/bin

Now, since the first directory in the path is ".", you could just type "a.out" to run your program. However, I could create a program that erases your entire hard drive, and call the file "ls". Then if you happened to be in the directory where I put that file and tried to get a directory listing, my program would run instead of the system "ls" that you were expecting.

(I just noticed that a couple others replied as I was typing this, so most of this has been said already in one way or another.)
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.