cancel
Showing results for 
Search instead for 
Did you mean: 
Trevor
Commander Commander
Commander
  • 356 Views

Internal vs External Commands

Jump to solution

I was doing some examination of internal (shell builit-ins) and external commands, and I saw something that has me puzzled.

The echo command is both an internal command, as well as an external command (/usr/bin/echo).  

Just for grins, I executed the following command:

           $  strace echo  hello

I was expecting to see output similar to:   strace: Cannot find executable 'echo'.  Instead, to my surprise, I saw that the external command (/usr/bin/echo) was used, along with all the wonderful system calls that were made.

Okay, for almost half a century, I've always known internal commands to have precendence over external commands.  With that being the case (if that is in fact the case), why was the external 'echo' invoked??????

I'm still learning

 

Trevor "Red Hat Evangelist" Chandler
Labels (3)
1 Solution

Accepted Solutions
Chetan_Tiwary_
Community Manager
Community Manager
  • 310 Views

commander @Trevor , you have dived into the multiverse of Dr Strace!

strace is not a shell so it cant interpret built in echo. It is made to look for an executable file and it did instead found /usr/bin/echo or /bin/echo:

Chetan_Tiwary__1-1755511659996.png

 

If you want to trace the built in echo, you are hereby directed to trace the shell itself :

Chetan_Tiwary__0-1755511645852.png

also, strace does not work with aliases or any function :

Chetan_Tiwary__2-1755511729996.png

So I think it is clear why precedence was overruled , because we broke it when we told strace to do "strace echo hello" .

So, when you pass echo to strace, it searches your $PATH for a regular executable file named echo (e.g., /usr/bin/echo or /bin/echo). It finds one and so it runs it.

If it couldn't, it wouldn't .

and then you would have got that error : strace: Cannot find executable 'echo'

Over to you commander ! 

View solution in original post

2 Replies
Chetan_Tiwary_
Community Manager
Community Manager
  • 311 Views

commander @Trevor , you have dived into the multiverse of Dr Strace!

strace is not a shell so it cant interpret built in echo. It is made to look for an executable file and it did instead found /usr/bin/echo or /bin/echo:

Chetan_Tiwary__1-1755511659996.png

 

If you want to trace the built in echo, you are hereby directed to trace the shell itself :

Chetan_Tiwary__0-1755511645852.png

also, strace does not work with aliases or any function :

Chetan_Tiwary__2-1755511729996.png

So I think it is clear why precedence was overruled , because we broke it when we told strace to do "strace echo hello" .

So, when you pass echo to strace, it searches your $PATH for a regular executable file named echo (e.g., /usr/bin/echo or /bin/echo). It finds one and so it runs it.

If it couldn't, it wouldn't .

and then you would have got that error : strace: Cannot find executable 'echo'

Over to you commander ! 

Trevor
Commander Commander
Commander
  • 291 Views

I do love these deep waters!!!!

This is pure gold!!!

I'm even celebrating the use of the "type -a echo" command.  I had "type echo" in my arsenal, but I can't say that I recall ever using the -a option.  Pure gold!!!

I don't know if I would have ever tried to strace an alias or a function, but one thing is for sure, I absolutely won't bother, now that I'm been informed/warned!!!

Finally, that method of tracing the internal echo is truly a bonus!  That's worth 2 gold bars all by itself!

Chetan, how much do I owe you for this one : - )

 

Trevor "Red Hat Evangelist" Chandler
Join the discussion
You must log in to join this conversation.