0

I compiled a silly little "hello world" C program called main.c:

gcc main.c

As expected, a file called a.out appeared, which they say is an executable. From that same directory, if I type

a.out

and hit enter, it says "command not found". But if I type

./a.out

It says "hello world", as desired. I've never seen an executable that requires a './' in front of it to run. Why now?

zkurtz
  • 3,230
  • 7
  • 28
  • 64

5 Answers5

2

All executables that aren't in your PATH require an explicit path from root / or the local directory ./ to run. A quick search turns up other threads with essentially the same question:

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

This also has the added benefit of helping with your auto completion in your shell (assuming it supports it). If you type just aTabTab then it will list every executable in your path that starts with "a". However, if you type ./aTab it will probably just auto-complete as a.out since it will only look at executable files in the current directory starting with "a". So, looking at it that way, the "./" actually saves you typing a few keys!

Community
  • 1
  • 1
DaoWen
  • 32,589
  • 6
  • 74
  • 101
0

That's because a.out is not in your $PATH.
The command you provide is searched in the $PATH (environment variable in linux) by the shell.

$PATH basically is the list of directories. When you provide the executable name, shell searches it in the directories provides by $PATH.

Since a.out is not in your $PATH, you've to explicitly provide the path to a.out.

mohit
  • 5,696
  • 3
  • 24
  • 37
0

It is standard practice in Unix and Linux not to have the current working directory in the path. If you want to have MSDOS/Windows behavior, alter your PATH variable to include . as the first directory.

wallyk
  • 56,922
  • 16
  • 83
  • 148
0

It's because the system is looking for a.out or any other exec. file in some special paths. And the current dir in not in that list by default (usually).

look at the list of such paths:

$ env|grep PATH

you can add such current dir to PATH env. variable:

$ export PATH=$PATH:.

But you better avoid doing that and run ./a.out. Such tech. provides us understanding that we are running specified file from current dir, not the other file with the same name from another (potentially) dir. So, we know what we run exactly.

Alex
  • 323
  • 2
  • 8
0

When you type something like a.out into a Linux terminal, you're implying that you want to run a command called a.out. By default, the terminal does not look in the current directory for these commands, it looks in PATH - a set of directories for executable programs. It is usually these directories:

  • /bin
  • /usr/bin
  • /usr/local/bin
  • among others (you can check them all by running echo $PATH)

You have to specifiy the directory directory of your program for it to run, if it is not in one of the directories of PATH. For example:

  • ./a.out works because . refers to the directory you're in
  • ../a.out could work if a.out is in a parent directory (.. refers to the parent) directory
  • projectdir/a.out also works, if your program is in the sub-directory, projectdir
tay10r
  • 4,234
  • 2
  • 24
  • 44