The Linux fg command

0 5

Have you ever wondered how you can send a job or process running in the background to the foreground on the Linux shell? The fg command, short for the foreground, is a command that moves a background process on your current Linux shell to the foreground. This contrasts the bg command, short for background, that sends a process running in the foreground to the background in the current shell.

The fg command & example usages

In this guide, we will demonstrate how to use the fg command.

Basic syntax

Additionally, you can invoke one of the symbols shown below to reference a job on the current shell.

%- – This represents a previous job

%% or %+ – This shows the current job.

%number – This represents the job number, for instance, %3 or %4.

%String – A job started by a command that comprises a string, e.g., %ping

We will simulate sending a job to the background and then bringing it to the foreground using the fg command.

So, launch your terminal and execute the ping command as shown such that it continuously sends ICMP echo requests without stopping.

Then interrupt it by pressing CTRL + Z. This causes it to stop running in the foreground of the current shell and run to the background. Use the jobs command to displays all jobs that are currently suspended or running in the background.

Take note of the output. Here, the 1 is job_spec

[1]+ 2547 Stopped                 ping google.com

Now, to bring the ping command to the foreground, use the fg command as follows:

From the output, you can see that we have brought the ping command back to the foreground. You can also run the command below, which brings to the foreground the previously suspended command.

For additional information about the fg command run:

Leave A Reply

Your email address will not be published.

For security, use of Google's reCAPTCHA service is required which is subject to the Google Privacy Policy and Terms of Use.

I agree to these terms.