Why doesn't a while or for loop get suspended when I type ^Z?

Why doesn't a while or for loop get suspended when I type ^Z?


This is a consequence of how job control works on Unix. The only
thing that can be suspended is the process group. This is a single
command or pipeline of commands that the shell forks and executes.

When you run a while or for loop, the only thing that the shell forks
and executes are any commands in the while loop test and commands in
the loop bodies. These, therefore, are the only things that can be
suspended when you type ^Z.

If you want to be able to stop the entire loop, you need to put it
within parentheses, which will force the loop into a subshell that
may be stopped (and subsequently restarted) as a single unit.



Home FAQ