8

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?

2

3 Answers 3

14

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.

8
  • 4
    The answer is only partly correct. The problem could be surrounded if the current directory was only used as last option. Then standard tools would still be found first. Apr 28, 2012 at 16:05
  • 1
    Also, shell builtins take precendence (i.e. when you type cd in bash, it executes a routine internal to bash and not /bin/cd) unless you prepend with `.`
    – LawrenceC
    Apr 28, 2012 at 21:43
  • 3
    One of the original *nix security measures, it forces system or admin compiled binaries that are in the system assigned path command to be executed. Nothing so bad as someone to log in as root on a help desk question, go to a user directory, type ls and find they loaded a bomb left by a user. Apr 28, 2012 at 22:03
  • 7
    @userunknown: And the first time you typoed sl, you've got the same problem. Only trusted directories should be in $PATH and relative paths like . are never a trusted directory for an interactive shell.
    – user35138
    Apr 28, 2012 at 22:35
  • 2
    @Basic: If I type rm -r target when I meant rm -f target there is usually no difference. If I type rm -f target when I meant rm -r target, either there's no difference or removal outright fails. Furthermore, I think every time before I type rm, especially if I'm adding -f or -r or a glob - it's a dangerous command by its nature. No one thinks before lsing.
    – user35138
    May 8, 2012 at 18:30
1

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

1

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.

1
  • I did not know we can add . to PATH, however, doing this will decrease security, right? Apr 29, 2012 at 6:04

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .