Why does the command "xterm xterm" create an infinite recursion?

I am running Ubuntu 14.04 (64 bit). When I first found out about the command xterm, I tried the command xterm xterm, and it started infinitely opening new xterm windows.
ctrl + C stops it immediately and closes all windows.

But I wanted to see how far it goes and let it run as long as it can. It ate up almost all the RAM and eventually got closed (I think by the system itself).

So just out of curiosity, why/how does this happen?


I (guess) this is because the first parameter you give to xterm is the shell to use - xterm bash (or xterm /bin/bash), xterm python etc.

So it runs xterm, tries to start xterm as a shell, which starts another xterm as that ones shell, then another, and another...

You can probably find a bit more on this by running man xterm


Short version: xterm's argument is the shell to execute by xterm; shell is set in environment var, so further calls do a 10 PRINT "xterm" 20 GOTO 10 recursion.

Long version:

  1. xterm xterm passes xterm to xterm call as xterm's shell by setting $SHELL variable to xterm (1st parameter of xterm is interpreted as shell to execute)
  2. then, xterm executed by your xterm xterm command executes the $SHELL - in this case, creating another xterm instance (because $SHELL=xterm now)
  3. $SHELL=xterm already, so the newly created xterm executes xterm
  4. goto 3

Further reading: man xterm