On an Ubuntu 12.04, with GNU bash, version 4.2.25(1)-release (x86_64-pc-linux-gnu),
I tried the following command:
$ bash -c 'pstree -s $$'
init───sshd───sshd───sshd───bash───pstree
$ bash -c 'pstree -s $$;echo'
init───sshd───sshd───sshd───bash───bash───pstree
I think the second one is as my expectation: the first bash
is where I executed these commands; the second bash
is what I started with bash -c ...
; and then the second bash will start a subprocess called pstree
.
However, I'm wondering what happened with the first one. Why the second bash
disappeared and the pstree
became a subprocess of the original bash
? And why the answer for the previous question doesn't apply to the second bash -c ...
?
Best Answer
I would have said that it is simply Tail Call Optimization, but in fact (as the last link points out),
bash
doesn't optimize tail calls. It only seems to optimize the case where the command to be executed is a simple command (that is, not a compound command).The second command is not a tail call of
pstree
, becausepstree
is not the last thing in the command line. (It's a tail call ofecho
, butecho
is usually a built-in so no subprocess will be created for it regardless.)To save reading all those links (although I hope they are interesting), the idea is that if you know that a function/program/whatever will return immediately after calling some other function/program/whatever, and that the value returned will be the value returned by the called function/program/whatever, then you might as well just reuse the current stack frame (or process, in the case of a shell script) instead of pushing a new stack frame (creating a new process), calling the function (running the script), and then returning. In a shell script, you can do that manually by using
exec
for the last command, but it would be possible for the shell to do that automatically.zsh
andksh
both seem to be able to do that, but notbash
:But that observation is just an observation, based on the versions of those shells which I happen to have installed, so YMMV: