If you're using Bourne-type
shells, you have to watch out for putting a series of commands
separated by semicolons (Section 28.16) into the background. These shells put only
the last command on the line into the background, but wait for the
first.
An easy way to test this is with the following command line, which
waits for 15 seconds, then does an ls:
$ sleep 15; ls &
In Bourne-like shells, you won't get your prompt
back until the sleep (Section 25.9) command has finished.
With Bourne-type shells, the proper way to put a series of commands
into the background is to group them with parentheses:
( ) Section 43.7
$ (sleep 15; ls)&
This may strike you as a defect, but in fact, it's a
sign of the greater precision of Bourne shell syntax, which makes it
somewhat exasperating for interactive use but much better for
programming.
It doesn't make any sense to run an interactive
program such as an editor in the background. For example, if you type
this from the C shell:
% vi &
[1] 3071
you'll get a message like the following:
[1] + Stopped (tty output) vi
vi can be active only in the foreground. However,
it does make sense to have vi
stopped (Section 23.1) in the background.
If you are
running vi or any other interactive program, you
can quickly get back to the shell by typing CTRL-z to stop the
program. The shell will take control of your terminal and print
another shell prompt.
Stopping vi (Section 23.6) is more efficient than using its shell escape mechanism (Section 17.21), since it lets you go back to your original
shell rather than starting a new one. Simply type
fg to get back to where you were in editing.