Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I found that sometime I use the command to run a command, but sometime I use the ./command to run another command.

What is the difference?

share|improve this question
    
I think that this question would become a bit more clear by substituting xx by command and ./xx by ./command. –  Exeleration-G Apr 3 '13 at 11:47
    
Thanks,I update the post. –  hguser Apr 3 '13 at 14:14
    
@Exeleration-G I'm not sure changing xx to command necessarily makes this clearer. Consider that command itself is a command (a shell builtin). Without special formatting (like "command") to show that command is a metasyntactic variable, I think xx is better. A more common metasyntactic variable is foo, but xx works just as well. –  Eliah Kagan Apr 3 '13 at 16:34
    
OK, well, xx is OK for me, too. –  Exeleration-G Apr 3 '13 at 17:38

1 Answer 1

Running xx searches the directories listed in the PATH environment variable for a file called xx. The first executable it finds (whether a binary or a script), it executes. It does not look in the current directory, unless . is listed in PATH, which is not recommended.

Running ./xx runs the file called xx that is located in the current directory, if there is an executable file with that name there.

To run a file by specifying its location, you must include a / symbol. If there is no / in the command (actually, the first word of the command, which is what identifies the name of the file to be executed), it searches PATH for it. If there is a /, then the first word of the command is taken to refer to a specific file by location.

Thus, ./xx is different from xx because of the / that it contains.

Of course, /xx would not work properly--it would try to run a file called xx located in the root directory, /. Because . represents the current directory, ./xx runs xx there.

share|improve this answer
    
So does it mean that ./xx is same as xx? –  hguser Apr 3 '13 at 5:07
    
No, it does not. It is also not what I read in the answer so why this comment? –  Rinzwind Apr 3 '13 at 7:13
    
I apologize, I mean if ./xx is the same as xx if the xx script is locatted at the current directory? –  hguser Apr 3 '13 at 7:52
1  
@hguser: ./xx and xx would be the same only if your current directory is on the $PATH and there wouldn't be any other executables named xx in dirs in $PATH before your cwd. –  progo Apr 3 '13 at 8:46
    
@hguser if you run which xx and it tells you /usr/bin/xx, then executing xx or cd /usr/bin ; ./xx will do the same... except for aliases or shell functions. For example, ls and cd /bin ; ./ls will probably show differences (the first with colors, the second without colors). That is because probably ls is an alias to ls --color. You can find if xx is an alias or is calling an executable in the disk using type xx. –  Carlos Campderrós Apr 3 '13 at 9:05

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.