bg Command in Linux

Written by: Nimesha Jinarajadasa   |   Last updated: August 8, 2022

In Linux, the bg command helps in managing jobs by enabling users to put existing jobs in the background and continue to run as a background job. This command is typically used when you have one or more processes running in the background, and you want to continue their execution without bringing them to the foreground.

A bit about how it works? If you have a process running in the foreground and you want to move it to the background, you can press Ctrl + Z to suspend (the suspended job will be assigned with a job id) the process and then use the bg command to resume it in the background. The shell will send a SIGCONT signal to the suspended process to resume its execution in the background.


The basic syntax for the bg command is as follows:

bg [job_spec]

job_spec: This is an optional argument that specifies the job to be sent to the background. It can be either a job ID or a process group ID.

Note: When used without any job_spec argument, it will either push the current foreground job into the background or resume a suspended job in the background, depending on the context of the shell session.

Using the type command you can check the source of the bg command is a useful way to determine whether it is a shell built-in or an external utility.

type bg

In this example, the bg command is available as a shell builtin.

bg command example

The bg command works with all the Linux shells except the bourne shell. Hence, we will be using the bash shell to execute the following commands. Since it operates on jobs associated with the shell, we need some jobs running. The jobs command displays the job id, process id, and status of the associated jobs with the current shell session.


This command returns nothing because we got no jobs associated with this shell session.

Let's initiate a sleep command to see how the bg command can be used to send a foreground job to the background.

sleep 60

This will create an empty job in the system memory for 60 seconds and the control over the command line will be lost immediately. Let's suspend the job by pressing the CTRL+Z keys on the keyboard as shown in the following.

sleep 60 seconds

Next, we will call the bg command to resume the suspended sleep job in the background. As discussed, the stopped job number should be passed to the bg command as follows. The job number is 1 in this example.

bg %1

Now the job identified by the ID 1 will be resumed and goes into the Running status. Let's verify it by executing the jobs command.

listing the jobs

Let's verify the behavior of the bg command when the job specification or job id is not passed. It should operate on the current job and resume it in the background. First, we will initiate a sleep command for 120 seconds and suspend it. The same procedure will repeat for another sleep job which times out after 240 seconds.

Finally, we will be calling the bg command without passing the job_spec(job ID) argument. So, the command should operate on the current job which is the sleep command issued with a timeout of 240.

bg command

As you could see, the immediate sleep command(with 240 timeout) has been resumed in the background.

bg options

The bg command comes with two main options listed as follows.

The job_spec option

A job associated with the current shell session can be referenced using a job_specification. The job_specification can be specified in different forms as follows. Furthermore, it should be started with a % character.

The %+ and %- characters can be used to reference the current job and the previous job associated with the current shell environment.

In referring to the previous example, We can use the following command to resume the previous sleep job which has a 120 seconds timeout.

bg %-

Furthermore, you can use the job id directly with the bg command. Let's resume the sleep job with 120 seconds timeout that is identified by job id 1. You should specify the stopped job number as follows.

bg %1

It is possible to reference a job by the command that is used to start it. Since the sleep command starts with the letter s, you can specify it as follows.

bg %s

In addition, the command which is used to start the job containing a string ā€˜sā€™ can be specified as follows.

bg %?s

About The Author

Nimesha Jinarajadasa

Nimesha Jinarajadasa

Nimesha is a Full-stack Software Engineer for more than five years, he loves technology, as technology has the power to solve our many problems within just a minute. He have been contributing to various projects over the last 5+ years and working with almost all the so-called “03 tiers(DB, M-Tier, and Client)”. Recently, he has started working with DevOps technologies such as Azure administration, Kubernetes, Terraform automation, and Bash scripting as well.



Please add comments below to provide the author your ideas, appreciation and feedback.

Leave a Reply

Leave a Comment