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?

share|improve this question
    
+1 Not sure why this valid question was down voted. – HeatfanJohn Aug 4 '13 at 20:11
1  
Wait, redhat includes ./ in $PATH by default? Ewwwwww... – Shadur Aug 5 '13 at 7:01
up vote 13 down vote accepted

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.

share|improve this answer
    
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. – Munai Das Udasin Aug 4 '13 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. – Thomas Nyman Aug 4 '13 at 17:27
    
Ok now i get it...thanks thomas! – Munai Das Udasin Aug 4 '13 at 17:28
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. – Shadur Aug 5 '13 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... – Munai Das Udasin Aug 5 '13 at 9:32

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.