Results 1 to 8 of 8
Hi all
I wanted to know the difference between these commands ,and when exactly they should be used?
/usr/bin/cp <file1> <file2>
cp <file1> <file2>
I have noticed that the first ...
- 05-26-2008 #1Just Joined!
- Join Date
- May 2008
- Posts
- 20
[SOLVED] difference btw these cmds and where should they be used?
Hi all
I wanted to know the difference between these commands ,and when exactly they should be used?
/usr/bin/cp <file1> <file2>
cp <file1> <file2>
I have noticed that the first method is exclusively used in boot scripts while the second is used predominantly once the terminal is up!
is it because during the boot process the system cannot detect the path to these executables or something else?
What is the main difference and where should these 2 types be used?
Thanks
- 05-26-2008 #2Linux Guru
- Join Date
- Nov 2004
- Posts
- 6,110
/usr/bin/cp is a self contained cp command that will work in any shell, whereas the cp command if not given the full path will default to the inbuilt cp of that shell. For example bash has its own cp and it will take priority over the external executable. If for instance you were to check the man page for for you would see that it refers to the C shell and uses syntax you may not be as familiar with. Bash again has its own inbuilt 'for' command.
It's really a matter of preference most of the time for this type of basic command but there is also the issue of environmental variables. If the user executing the command did not have /usr/bin/ in their $PATH then they would not be able to execute cp if their default shell was sh for example.
- 05-26-2008 #3Just Joined!
- Join Date
- May 2008
- Posts
- 20
oh ok,
Thanks a lot ,now i got the entire picture...
I was searching for such things on the web but efforts were in vain..
Thanks a lot!!!1
- 05-26-2008 #4Um, not quite. True for bash and echo (for example), but not bash and cp. Here's how you can tell. Do this at the command line:the cp command if not given the full path will default to the inbuilt cp of that shell. For example bash has its own cp and it will take priority over the external executable.
That will tell you where the separate program "echo" is found on your system. Depending on your Linux distribution, it is usually found at /bin/echo or /usr/bin/echo. For this discussion, let's assume that it's at /usr/bin/echo. Now try these two commands:Code:which echo
See how they respond differently? The first one is the bash builtin. The second one is the separate program.Code:echo --version /usr/bin/echo --version
You can also do this:
That forces bash to use the builtin. (Be assured that there are circumstances when you would want to spell this out.)Code:builtin echo --version
Now let's assume that the separate program "cp" is also at /usr/bin/cp. (You'll want to do
to tell for sure.) Try these three commands:Code:which cp
You'll find that the first two commands give exactly the same response, and the third complains that cp is not a shell builtin command.Code:cp --version /usr/bin/cp --version builtin cp --version
To answer your original question, do this at the command line:
This will give you not one path, but a list of them, separated from each other by a colon (":"). This tells bash where to look for programs which are to be run. But that's only if bash needs a separate program.Code:echo $PATH
Here's how that works. When you give a bash command, it does one of five things with it. It tries them in this order.
- If it's an alias, it will execute that alias. You can enter this command:
to find out which aliases you've set up (or which were set up for you automatically when you logged in). You can remove any of those, and add others. To find out how, do this at the command line:Code:alias
Code:help alias
- If it's a keyword, such as function, if, or for, it will handle that keyword.
- If it's a function, it will execute that function. You can enter this command
for more information.Code:help function
- If it's (ta da!) a builtin, such as cd, echo, or type, it will execute that builtin.
- If it's an executable program that's found by searching the list of paths in your PATH environment variable, bash will execute that.
And the reason you find program names fully spelled out with the paths at which they're located is that in boot scripts the PATH environment variable is not available.
Questions?--
Bill
Old age and treachery will overcome youth and skill.
- If it's an alias, it will execute that alias. You can enter this command:
- 05-26-2008 #5Linux Guru
- Join Date
- Nov 2004
- Posts
- 6,110
- 05-27-2008 #6Just Joined!
- Join Date
- May 2008
- Posts
- 20
Oh so since the PATH variable isnt set,we give the absolute pathname is it?
Then how come commands like echo work without the actual path being specified
eg : echo "this is a test" >/dev/msglog
this logs an o/p subsequently,however its never stated as /usr/bin/echo <something>
Here you mean to say that its accessed differently?
Also this brings to an intriguing que, how about the looping statements?
How are they accessed?
Thanks?
- 05-27-2008 #7In boot scripts for commands which aren't builtin, yes.Oh so since the PATH variable isnt set,we give the absolute pathname is it?
That will work in a boot script because it's a builtin. It will work at the command prompt because it's a builtin, and even if it weren't a builtin, at the command prompt you typically have the PATH variable set so that the separate "echo" program can be found.Then how come commands like echo work without the actual path being specified
They are always accessible because they're an integral part of bash, just as the builtin commands are. (You can "hide" them by aliasing them to something else, but few people are that unconventional.)Also this brings to an intriguing que, how about the looping statements?
How are they accessed?--
Bill
Old age and treachery will overcome youth and skill.
- 05-27-2008 #8Just Joined!
- Join Date
- May 2008
- Posts
- 20
This clears all my queries...
Thanks a lot



