Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

In Unix, when 2 programs have the same name:

  1. The one that is found earliest will be executed
  2. The system program will be executed over the user program
  3. The user program will be executed over the system program
  4. There is a file system error

Which of the above is true?

share|improve this question
8  
Sounds like a homework assignment... –  patrix May 8 '13 at 17:30

3 Answers 3

1) This isn't a UNIX thing, it really depends on your shell. bash on Windows would perform the same.

2) It will be the one first in your PATH. The shell has no idea what a "system path" vs. a "user path" is. It's all one PATH.

3) Specifically for bash (again, not UNIX, but bash) there is a hashing mechanism to speed up lookup of commands. If you type, say, ls, it will look for ls in the command hash. If the bash shell finds ls in the hash, it will run the one in the hash. So, if you ran ls, and /bin/ls was the first in your PATH, it would hash /bin/ls. Now, even if you had a $HOME/bin/ls and $HOME/bin was first in your PATH, it would still find it in the hash, and run /bin/ls

So the shell will use whatever is first in PATH, unless it's overridden by a command path cache (called the hash in bash).

share|improve this answer
    
I thought ls was built into bash. –  CyberSkull May 8 '13 at 18:37
    
@CyberSkull, no, ls is not built-into to bash. It generally comes as part of cureutils, unless you are using busybox. dpkg -S /bin/ls returns coreutils: /bin/ls. –  Zoredache May 8 '13 at 18:57
    
@CyberSkull you may be thinking "echo" which you can list files with "echo *" –  Rich Homolka May 8 '13 at 19:19
    
I'll have to wait until I get home to look up the built-in shell commands. –  CyberSkull May 8 '13 at 20:54
    
I was wrong, the built in command in bash is ls-F. –  CyberSkull May 14 '13 at 9:20

If the programs are in different directories and both directories are on your path, the one that is found first (i.e., is in a directory that is looked in fist) will be executed. The system searches your path and executes the first executable file it finds matching the name you've entered.

If the programs are in the same directory, they can't have the same name.

share|improve this answer

It depends on the order of your PATH environment variable. Type 'echo $PATH' in your terminal to see the order. If you have aliases that is another matter since aliases are used before the actual program. Type 'alias' to see those. Many *nix platforms have an alias for ls to support color coding. The 'which [program]' command will tell you what program will be executed.

share|improve this answer

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.