Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I would like to know what is the behaviour of Ctrl+Z and fg in bash when wanting to append commands to be executed after a running command has finished. For example, in the sequence for commands below, I would expect the console to display "1", then "2", then "3", then "4", but I only get the last command, echo 4, after sleep 30 finishes:

avilella@magneto:~$ sleep 30 && echo 1 
^Z
[1]+  Stopped                 sleep 30
avilella@magneto:~$ fg && sleep 5 && echo 2
sleep 30
^Z
[1]+  Stopped                 sleep 30
avilella@magneto:~$ fg && sleep 5 && echo 3
sleep 30
^Z
[1]+  Stopped                 sleep 30
avilella@magneto:~$ fg && sleep 5 && echo 4
sleep 30
4

Any ideas?

share|improve this question
    
What's the real problem? Are you interested in how ^Z & fg work, or are you trying to script a solution to a specific problem? –  PriceChild Nov 27 '11 at 0:18
    
why don't you run jobs after every Ctrl-Z and see what you have running –  Rich Homolka Nov 27 '11 at 0:50

1 Answer 1

Control-Z suspend a job (not your shell command line), so all the other commands will run without waiting for it. Seems that when you stop a job the shell try to run the next one but as you used && and the previous command was not finished OK (was not finished at all but paused), the command with && is discarded. On the other hand if you use ; instead && for a second command in the command line, then you would see that the second command gets executed when the first one is paused:

$ perl -e '$|=1;for (1..5){ sleep 1;print}print qq{END\n} ';echo test_z00000000000000
12^Z
[1]+  Stopped                 perl -e '$|=1;for (1..5){ sleep 1;print} '
test_z00000000000000
$ fg
perl -e '$|=1;for (1..5){ sleep 1;print} '
345END
$ 
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.