I'm trying to understand some kernel level concepts in Linux. I was checking on the difference between shell builtin commands and the other executable commands.

This wonderful answer clearly tells the difference and also specifies the need for shell builtin commands.

Now I know that using type <command-name>, I could check if it is an external or shell builtin command.

So I decided to do some strace'ing on the various commands to understand more of the internals.

I learned this neat little trick to do strace'es on shell builtin commands. I was able to do strace on cd .. as well according to the above answer.

Now when I run type pwd and get the output as, pwd is a shell builtin. So, I expect that I wouldn't be able to run strace on it, since it too is a shell builtin. But when I did an strace on it, I was surprised to see that strace worked without the need to do stty.

I verified strace for echo as well and it worked fine too.

So my understanding is, that strace worked in the case of pwd and echo because the execution of pwd and echo did not change any of the shell's behavior.

Am I correct in my understanding?

  • Many of the commands are available as built-ins and as standalone executables. echo and pwd are 2 such commands, so you're stracing the standalong counterparts and not the built-ins when you did the above tests. – slm Sep 9 '14 at 2:17
  • @slm, thanks. I realized after I found that I could find the type of command using type -a. – Ramesh Sep 9 '14 at 2:21
  • @slm, so if I create a standalone for cd and do the strace, chances are that strace cd will work without stty. Is this understanding correct? – Ramesh Sep 9 '14 at 2:33
  • Yes, if you have an executable, then you can strace it. But aliases and functions etc. are built into Bash and cannot be straced, since you'd have to strace Bash itself to watch them. – slm Sep 9 '14 at 2:35
  • Re-read the man page for strace. There are no system calls to trace for a builtin since it's doing everything internal to Bash. – slm Sep 9 '14 at 2:37

Because pwd or echo have external commands with the same name, /bin/pwd or /bin/echo. If you look at strace output, you can see:

$ strace pwd
execve("/bin/pwd", ["pwd"], [/* 68 vars */]) = 0
brk(0)                                  = 0x241e000
access("/etc/ld.so.nohwcap", F_OK)      = -1 ENOENT (No such file or directory)
mmap(NULL, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f80c31b9000

A note that when searching in PATH, strace accepts only regular file with execute bits set.

  • thanks but type pwd gives me it as shell builtin. – Ramesh Sep 9 '14 at 1:40
  • @Ramesh: Use type -a pwd will give you a full output. – cuonglm Sep 9 '14 at 1:41
  • great. So if we have an alias or function along with a shell builtin, in those cases, strace might work. Am I right in my understanding? – Ramesh Sep 9 '14 at 1:45
  • @Ramesh: No, strace doesn't work with alias, or function. – cuonglm Sep 9 '14 at 1:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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