Why I have to type "./" always before an executable to execute it in Linux?

When I compile e.g. x.c by:

GCC x.c -o x

To execute result I have to write:

./x

And just typing:

x

Fails with this message:

x: command not found

This means Linux does not search current directory for it! Is there (I'm sure there is) any reasonable issue behind this behaviour?


Solution 1:

Because by default, the current working directory is not in the PATH variable.

This is a security/convenience measure. If you have binaries/scripts called e.g. cd, ls, etc. in your current working directory, it would be very annoying if they were run by default.

Solution 2:

It means that the current directory is not in the $PATH variable.

Solution 3:

You can fix this by adding the current directory (represented by a single dot) to the PATH environment variable.
The way to do it depends on the shell you are using.
if you are using bash, you can add the line export PATH=$PATH:. to the .bashrc file in your home directory.
if you are using csh or tcsh add the line set PATH = ($PATH .) to the file .cshrc in your home directory.
IMHO, for a home desktop computer this is an acceptable thing to do - security wise.