Linux Commands

The Linux fg command

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

$ fg  job_spec

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.

$ ping

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.

$ jobs -l

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

[1]+ 2547 Stopped                 ping

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

$ fg %1

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.

$ fg %-

For additional information about the fg command run:

$ fg --help

About the author

Karim Buzdar

Karim Buzdar holds a degree in telecommunication engineering and holds several sysadmin certifications. As an IT engineer and technical author, he writes for various web sites. He blogs at LinuxWays.