4 ms·
Try doing the Ctrl-z/fg sequence in this: "while true ; do sleep 1 ; done". Under Linux at least, you'll see that after 'fg', the loop ends :-( Apparently C-z
by ttsiodras 12y ago
Try doing the Ctrl-z/fg sequence in this: "while true ; do sleep 1 ; done". Under Linux at least, you'll see that after 'fg', the loop ends :-(
Apparently C-z followed by fg is not bulletproof...
- chaosfox 12y agoThats an interesting example. zsh does the right thing, when you bg it back it recovers the loop as it was. Bash seems to forget about the loop and only recovers the "sleep 1", when it starts executing again it sleeps 1 and exits. If you want to make sure it will work you should wrap it around another bash: bash -c 'while true ; do sleep 1 ; done' should work fine.
- __david__ 12y agoParens work too: (while true ; do sleep 1 ; done)
- zilog80 12y agoI tried it, and you're right - but why does this happen?
- zwp 12y agowhile, true, do, done are all [bash] shell builtins. They are not running processes that can be suspended. sleep(1) is the only process that can be suspended in this command. When you "fg", the suspended sleep(1) is signaled, and resumes. And then it exits. When you do this in a subshell (in parens), you suspend/resume the subshell in its entirety, which capture the loop construct. Why does it work in zsh?!