When working in the BASH environment, you can start multiple programs from the same prompt. Each program is a job. Whenever a job is started, it takes over the terminal.
On today’s machines, the terminal is either the straight-text interface you see when you boot the machine or the window created by the X Window System on which BASH runs.
(The terminal interfaces in X Window System are called a pseudo-tty, or pty for short.) If a job has control of the terminal, it can issue control codes so that text-only interfaces (the Pine mail reader, for instance) can be made more attractive. Once the program is done, it gives full control back to BASH, and a prompt is re-displayed for the user.
Not all programs require this kind of terminal control, however. Some, including programs that interface with the user through the X Window System, can be instructed to give up terminal control and allow BASH to present a user prompt, even though the invoked program is still running.
[[email protected] ~]$ firefox &
Immediately after you press enter, BASH will present its prompt again. This is called backgrounding the task.
If a program is already running and has control of the terminal, you can make the program give up control by pressing ctrl-z in the terminal window. This will stop the running job (or program) and return control to BASH so that you can enter new commands. At any given time, you can find out how many jobs BASH is tracking by typing this command:
[[email protected] ~]$ jobs + Running firefox &
The running programs that are listed will be in one of two states: running or stopped. The preceding sample output shows that the Firefox program is in a running state. The output also shows the job number in the first column—.
To bring a job back to the foreground, i.e., to give it back control of the terminal, you would use the fg (foreground) command, like this:
[[email protected] ~]$ fg number
where number is the job number you want in the foreground. For example, to place the Firefox program (with job number 1) launched earlier in the foreground, type
[[email protected] ~]$ fg 1 firefox
If a job is stopped (i.e., in a stopped state), you can start it running again in the background, thereby allowing you to keep control of the terminal and resume running the job. Or a stopped job can run in the foreground, which gives control of the terminal back to that program.
To place a running job in the background, type
[[email protected] ~]$ bg number
where number is the job number you want to background.
NOTE You can background any process if you want to. Applications that require terminal input or output will be put into a stopped state if you background them. You can, for example, try running the top utility in the background by typing top &. Then check the state of that job with the jobs command.