Can’t run C program: “a.out: command not found”

When you type a command name (a.out is no different from any other command name in this respect), the shell searches for an executable file with that name. It performs this search using a list of directory names stored in your $PATH environment variable.

You can see your current $PATH by typing

echo $PATH

at your shell prompt. A typical value might be something like

/usr/bin:/bin

though you’ll probably have some additional directories as well.

Since a.out is in your current working directory (type pwd to see what directory that is), and your current working directory probably isn’t in your $PATH, you can’t execute it by typing just a.out.

Since you can refer to your current directory as ., you can (and should) execute the command by typing

./a.out

NOTE: You can have . in your $PATH, but it’s considered a bad idea to do so, since it makes it too easy to execute random commands accidentally. If . is at the front of your $PATH, imagine that I ask you to cd to my directory and type ls — but I’ve installed a file called ls that does something nasty. Putting . at the end of your $PATH alleviates that risk, but it doesn’t eliminate it entirely. It’s best to cultivate the habit of preceding a file name with ./ if you want to execute it from the current directory.

(I’ve ignored the fact that aliases, functions, and shell built-in commands can also be executed this way.)

Leave a Comment