How to Manage Jobs in Bash Shell with bg, fg, jobs commands

This post describes how to manage jobs in the Bash shell. The Bash shell enables you to run jobs in the background, which frees your terminal to perform other commands. You can also run jobs in the foreground. You can use Bash shell commands to list current jobs as well as to stop a job.

Introducing Jobs

A job is a process that the shell can manage. Shells start and control jobs. Because jobs are processes, each job has an associated PID. The shell also assigns each job a sequential job ID number. The shell enables you to run multiple jobs at the same time. Job control commands enable you to manage multiple jobs within a shell. There are three types of jobs that shells manage: foreground jobs, background jobs, and stopped jobs.

When you perform a command in a terminal window, the command occupies that terminal window until it completes. This type of job is called a foreground job. When you enter an ampersand (&) symbol at the end of a command line, the command runs without occupying the terminal window. The shell prompt is displayed immediately after you press Return. This type of job is called a background job.

If you press Control-Z for a foreground job, or perform the stop command for a background job, the job stops. This job is called a stopped job. Job control commands enable you to place jobs in the foreground or background, and to start or stop jobs. The table below describes the commands you can use for job control.

Command Value
jobs Lists all jobs that are currently running or are stopped in the background
bg %n Runs the current or specified job in the background (n is the job ID)
fg %n Brings the current or specified job into the foreground (n is the job ID)
Control-Z Stops the foreground job and places it in the background as a stopped job
stop %n Stops a job that is running in the background (n is the job ID)

Running a Job in the Background

To run a job in the background, enter the command you want to run along with an ampersand (&) symbol at the end of the command line. For example, you can run the sleep command in the background as follows:

$ sleep 500 &
[1]     3028
$

NOTE: The sleep command suspends execution of a program for n seconds.

The shell returns the job ID number it assigned to the command, contained in brackets, and the PID associated with the command. You use the job ID number to manage the job with job control commands. The kernel can use the PID number to manage the job.

When a background job has finished and you press Return, the shell displays a message indicating that the job is done.

[1] +  Done                    sleep 500 &
$

Listing Current Jobs

You can use the jobs command to display the list of jobs that are running or stopped in the background. For example:

$ jobs [1] +  Running                 sleep 500 &
$

Bringing a Background Job Into the Foreground

You can use the fg command to bring a background job to the foreground. For example:

$ fg %1
sleep 500

Sending a Foreground Job to the Background

You can use the Control-Z keys and bg command to return a job to the background. The Control-Z keys suspend the job, and place it in the background as a stopped job. The bg command runs the job in the background. For example:

$ sleep 500
^Z[1] + Stopped (SIGTSTP)        sleep 500

$ jobs
[1] + Stopped (SIGTSTP)        sleep 500

$ bg %1
[1]     sleep 500&

$ jobs
[1] +  Running                 sleep 500
$
command1 $(command2)

To ignore the special meaning of the dollar sign metacharacter, use the following command:

$ echo ’$SHELL’
$SHELL