31

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?

Pabi
  • 7,401
  • 3
  • 40
  • 49
CluelessNoob
  • 2,275
  • 3
  • 24
  • 29
  • 3
    Off-topic: it was closed because it probably used too much memory and got killed by the OOM (out-of-memory) killer. – Léo Lam Jun 27 '14 at 15:09
  • 5
    This should be fixed by version 301: "only set SHELL environment variable to programs found in /etc/shells (prompted by patch/report by Al Poole)". (The answers explain what's with the SHELL environment variable). – Cristian Ciupitu Jun 28 '14 at 18:39
  • @CristianCiupitu - +1 - I just installed version 308 - and instead of opening recurring terminals, it opens a blank one, and a second one with bash (the second presumably being the 'shell' of the first xterm. – Wilf Jul 02 '14 at 14:13

2 Answers2

26

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

Wilf
  • 30,194
  • 17
  • 108
  • 164
  • 24
    Yes. The first xterm sets $SHELL to xterm and starts another xterm as its shell. That other xterm uses the command in $SHELL as it's shell thus starting another xterm, ... – Florian Diesch Jun 27 '14 at 13:50
  • 6
    This part of the answer isn't clear: "which starts another xterm as that ones shell". Why does that happen? Thanks @FlorianDiesch for explaining that part. – John Kugelman Jun 27 '14 at 17:23
  • @JohnKugelman - the first xterm starts the second xterm as its shell, the second xterm starts the third xterm as its shell, the third starts the fourth as its shell... etc – Wilf Jun 27 '14 at 18:48
  • 3
    I get why the first xterm starts the second xterm: it's because you typed xterm xterm. But why does the second xterm start the third xterm? You didn't type xterm xterm xterm, so it's not obvious why the third xterm starts. Florian's comment explains why. – John Kugelman Jun 27 '14 at 19:02
  • @FlorianDiesch: I think you should post that as a separate answer, since it gives the real explanation. – Nate Eldredge Jun 28 '14 at 01:12
  • 1
    So just out of curiosity it is also possible with gnome-terminal ? – TuKsn Jun 28 '14 at 13:05
  • @Tuknutx: Interesting, I tried this: gnome-terminal --command=gnome-terminal . But it doesn't seem to infinitely create windows. It just opens one extra terminal. So apparently it does not work like xterm. But maybe there is a way to make it behave like that. – CluelessNoob Jun 28 '14 at 16:27
  • @LonelyKing - thats because thats gives it a command for it to run, not the shell for it to use - this might be possible if you could specify the shell just for gnome-terminal. – Wilf Jun 28 '14 at 16:34
  • @Wilf Right! :) – CluelessNoob Jun 28 '14 at 17:13
16

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

Cristian Ciupitu
  • 165
  • 1
  • 16