11

My Confusion

a.out is the output of the programs which I execute in my Ubuntu 12.10. In Red Hat system when I execute a.out in the terminal it executes. While in Ubuntu I have to execute ./a.out to get the output. 'a.out' doesn't work.

Can somebody explain what is the difference between the commands?

2
  • 2
    +1 Not sure why this valid question was down voted. Commented Aug 4, 2013 at 20:11
  • 6
    Wait, redhat includes ./ in $PATH by default? Ewwwwww... Commented Aug 5, 2013 at 7:01

1 Answer 1

18

The behaviour you experience depends most likely on differences in the environment variable $PATH. The $PATH is essentially a colon-separated list of directories, which are searched in order for a particular executable when a program is invoked using anexec operating system call. The $PATH can contain relative path components, typically . or an empty string, which both refer to the current working directory. If the current directory is part of $PATH, files in the current working directory can be executed by just their name, e.g. a.out. If the current directory is not in $PATH, one must specify a relative or absolute path to the executable, e.g. ./a.out.

Having relative path components in $PATH has potential security implications as executables in directories earlier in $PATH overshadow executables in directories later in the list. Consider for example an attack on a system where the current working directory path . preceeds /bin in $PATH. If an attacker manages to place a malicious script sharing a name with a commonly used system utility, for instance ls, in the current directory (which typically is far easier that replacing binaries in root-owned /bin), the user will inadvertently invoke the malicious script when the intention is to invoke the system ls. Even if . is only appended at the end of $PATH, a user could be tricked to inadvertently invoke an executable in the current directory which shares a name with a common utility not found on that particular system. This is why it is common not to have relative path components as part of the default $PATH.

7
  • Hey thomas.. I am trying to execute a.out in Unbuntu 12.10 from the same file it is stored. But still it doesn't work. I have to use ./a.out unlike in redhat. Commented Aug 4, 2013 at 17:15
  • If you do echo $PATH on each of the systems you should see . as part of of the PATH on Red Hat, but not on the Ubuntu system. If you want the same behaviour on Ubuntu you could append . to the PATH on Ubuntu with the command export PATH=$PATH:. (add PATH=$PATH:. to ~/.profile to make the change permanent), but be aware that this has certain security implications which I try to explain in the edited answer. Commented Aug 4, 2013 at 17:27
  • 1
    +1 for the security warning of having ./ in $PATH. It's a bad idea from a security point of view and it encourages sloppy behavior you really don't need. Commented Aug 5, 2013 at 7:03
  • hi thomas.... echo $PATH in red hat has no . in the end of the path name as you said echo $PATH gave me this in fact /usr/kerberos/bin:/usr/local/bin:/bin:/usr/bin::/opt/oracle/product/11.2.0/clien t_1/bin:/home/H11/u756938/bin and a.out is still working... Commented Aug 5, 2013 at 9:32
  • 1
    Both . and an empty string in $PATH refer to the current directory. Note the :: after /usr/bin in the output of echo $PATH. Commented Aug 6, 2013 at 4:33

You must log in to answer this question.

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