4

I did the following in BASH:

while true;do bash;done

I wrote this one liner, but I was not sure at first whether it:

  • stays in the main shell and fathers as many subshells as it runs dry of memory or some other stuff.
  • the main shell fathers a subshell, than this subshells fathers a subshell until this lineage runs dry of memory or some other stuff.

But, I suppose it is the second case, because once I run the one liner, I got shortly after my prompt back and I began to type exit and another exit and exit, exit, exit...and I still was not back in the main shell.

Now, since so many subshells have been opened and each one is a program, I thought each one should have its own PID.

So I did:

ps aux | grep bash

expecting to see a lot of processes with bash in their names.

However, nothing like this, there were only two bash shells.

How is it possible, I guess I have somewhere a very wrong idea of processes, shells, subshells and PIDs, but do not know where.

  • Does s.o. know how to make a shell spawn many shells in parallel or sequence? – sharkant May 12 '17 at 15:25
  • 2
    The term you appear to be looking for is a "fork bomb". Handle with care, as fork-bombing yourself is rarely much fun. – amalloy May 12 '17 at 20:29
  • thank you, I was not aware it has such a dramatic name – sharkant May 12 '17 at 20:34
  • @sharkant f() { f() & f() &;};f – Joshua May 12 '17 at 21:46
6

Your loop starts one single interactive shell session in each iteration.

The interactive session gives you a prompt (since it's interactive). Exiting it gives the control back to the loop, which starts another interactive shell.

This is why you don't get a massive amounts of bash processes at once, only two, the parent which runs the loop, and the child which gives you the new prompt.


The loop may also be written

while true; do bash; done

The true utility does not take an argument, it just returns a zero exit value which is interpreted as "true" by the shell.


If you had wanted an explosion of bash processes (a so called "fork-bomb"), you may have written

while true; do { bash & }; done

This would start an interactive bash session in each iteration of the loop, but as background processes. Since the sessions are started as background processes, the loop will not wait for the latest bash process to finish before starting the next one.

You may have to reboot your system to recover from this.

This fork-bomb is more benign than what it could be, as it only starts one new process in each iteration. Others may well start processes in an exponential fashion.

  • This makes totally sense, at least visually, each iteration kicks me out of the loop and when I type exit I get back in the loop just to get kicked out again. I heard the term but I do not really know what an "interactive shell" is, but if it was an "noninteractive shell" would it work to spawn a plethora of subshells? – sharkant May 12 '17 at 14:44
  • 2
    @sharkant An interactive shell is a shell that you type commands into. A non-interactive shell is a shell that performs a task without having to prompt the user for input (like running a shell script). Try replacing bash with bash -c 'echo hello' (which runs bash as a non-interactive shell) to see the difference. Press Ctrl+C (maybe a few times) to interrupt the loop if you test this. – Kusalananda May 12 '17 at 14:47
  • 2
    @sharkant You still only get a total of two shells at a time though. The non-interactive shell does what it's suppose to do and exits before the loop starts the next one. – Kusalananda May 12 '17 at 14:50
6

Your loop is starting an endless number of shells, but one after the other, not in parallel. The first time the loop runs, it runs bash, which starts a new shell and displays the prompt. The parent shell waits for that shell to exit; this happens when you type exit, exiting the child shell and returning to the parent shell, which goes round the loop again and runs bash, which starts a new shell and displays the prompt...

You can display your shell level by running

echo $SHLVL

You’ll see this number doesn’t change when you type successive exit commands.

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.