How to use fg command in Linux.

How To Use Fg Command In Linux.


$ 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